Union Graph Flashcards

(2 cards)

1
Q

Number of Islands
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.

A
  1. Do a DFS from every location that has “1”
  2. Modify the matrix to set those cells visited to “0” to prevent double counting
  3. Return the number of DFSs

If modifying matrix is not option, hold a set of visited locations

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
2
Q

Number of Islands II

A
How well did you know this?
1
Not at all
2
3
4
5
Perfectly