From 5b482ee1b5e5ecf48430dc72ebb9690057505671 Mon Sep 17 00:00:00 2001 From: sidaksohi <31377210+sidaksohi@users.noreply.github.com> Date: Sun, 22 Aug 2021 15:58:47 -0700 Subject: [PATCH 1/2] Add number_of_islands, with description --- searches/number_of_islands.rb | 79 +++++++++++++++++++++++++++++++++++ 1 file changed, 79 insertions(+) create mode 100644 searches/number_of_islands.rb diff --git a/searches/number_of_islands.rb b/searches/number_of_islands.rb new file mode 100644 index 0000000..2fd107c --- /dev/null +++ b/searches/number_of_islands.rb @@ -0,0 +1,79 @@ +#Given an m x n 2D binary grid grid which represents a map of '1's (land) and '0's (water), return the number of islands. +#An island is surrounded by water and is formed by connecting adjacent lands horizontally or vertically. You may assume all four edges of the grid are all surrounded by water. + +#Example 1: +#Input: grid = [ +# ["1","1","1","1","0"], +# ["1","1","0","1","0"], +# ["1","1","0","0","0"], +# ["0","0","0","0","0"] +#] +#Output: 1 + +#Example 2: +#Input: grid = [ +# ["1","1","0","0","0"], +# ["1","1","0","0","0"], +# ["0","0","1","0","0"], +# ["0","0","0","1","1"] +#] +#Output: 3 + +#Constraints: +#m == grid.length +#n == grid[i].length +#1 <= m, n <= 300 +#grid[i][j] is '0' or '1'. + + + + +#DFS, Recursive Bottom Up Approach - O(n^2) Time / O(1) Space +#Init num_of_islands = 0, return if the grid is empty +#Start a double loop with index to iterate through each plot (each value is a plot of either water or land in this case) +#if the plot is land, dfs(grid, x, y) +#num_of_islands += 1 +#Return num_of_islands + +#dfs(grid, x, y) +#Return if x or y are out of bounds, or if the plot is water +#Make the current plot water +#Call dfs again for up, down, left, and right + +# @param {Character[][]} grid +# @return {Integer} +def num_islands(grid) + return 0 if grid.empty? + + #init num of islands + islands = 0 + + #loop through each element (plot) in the 2d array + grid.each_with_index do |row, x| + row.each_with_index do |plot, y| + #if the plot is water, start a dfs + if plot == "1" + dfs(grid, x, y) + #add 1 to islands once all connected land plots are searched + islands += 1 + end + end + end + + #return ans + islands +end + +def dfs(grid, x, y) + #don't search if out of bounds, or if it's already water + return if x < 0 || x >= grid.length || y < 0 || y >= grid[0].length || grid[x][y] == "0" + + #set the plot to water + grid[x][y] = "0" + + #search each adjacent plot + dfs(grid, x - 1, y) #up + dfs(grid, x + 1, y) #down + dfs(grid, x, y - 1) #left + dfs(grid, x, y + 1) #right +end From 0df030f25afd8f097c6cd7a7ac9047f180ac06f4 Mon Sep 17 00:00:00 2001 From: sidaksohi <31377210+sidaksohi@users.noreply.github.com> Date: Sun, 22 Aug 2021 21:42:12 -0700 Subject: [PATCH 2/2] Update number_of_islands.rb --- searches/number_of_islands.rb | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/searches/number_of_islands.rb b/searches/number_of_islands.rb index 2fd107c..b782c41 100644 --- a/searches/number_of_islands.rb +++ b/searches/number_of_islands.rb @@ -28,7 +28,7 @@ -#DFS, Recursive Bottom Up Approach - O(n^2) Time / O(1) Space +#DFS, Recursive Bottom Up Approach - O(n*m) Time / O(1) Space #Init num_of_islands = 0, return if the grid is empty #Start a double loop with index to iterate through each plot (each value is a plot of either water or land in this case) #if the plot is land, dfs(grid, x, y)