Skip to content

Latest commit

 

History

History
11 lines (8 loc) · 565 Bytes

File metadata and controls

11 lines (8 loc) · 565 Bytes

dfs

We can go through all the nodes in the grid. We can use dfs to traverse each of the connected area from the node that is 1, and we will mark visited for the nodes we traversed.

time: O(m * n) - we will traverse the nodes of all '1'
space: O(m * n) - call stack of all number of nodes in the grid and the visited grid

bfs

We can go through all the nodes in the grid. We can use dfs to traverse each of the connected area from the node that is 1, and we will mark visited for the nodes we traversed.

time: O(m * n)
space: O(m * n) - mark visited