Skip to content

Instantly share code, notes, and snippets.

@fbaierl
Last active August 14, 2018 08:29
Show Gist options
  • Save fbaierl/f34d419a7e94121382a18bd5da2622af to your computer and use it in GitHub Desktop.
Save fbaierl/f34d419a7e94121382a18bd5da2622af to your computer and use it in GitHub Desktop.
NumberOfIslands
/*
https://leetcode.com/problems/number-of-islands/description/
Given a 2d grid map of '1's (land) and '0's (water), count 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:
11110
11010
11000
00000
Output: 1
Example 2:
Input:
11000
11000
00100
00011
Output: 3
*/
object Solution {
def numIslands(grid: Array[Array[Char]]): Int = {
// cells we have already visited, only stores '1' cells
var used : List(Int, Int) = List()
// number of found islands
var result = 0
// TODO
// for each cell
// if '1' & not in used:
// -> BFS with queue to find adjacent '1's
// -> add every visited '1' cell to used
// -> result++
result
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment