Loading Similar Posts
Given a M x N grid map, each cell of grid is '1' if there is land, '0' if there is water. Print number of Islands.
An island is surrounded by water and is formed by connecting adjacent lands (vertically or horizontally). You can assume outside matrix is completely filled with water.
We can treat given grid as an , assuming each grid cell is a node, and two nodes are connected if both of them are land and adjacent in grid.
Now required answer is simply in our graph. We can do it by using DFS, BFS or DSU (Disjoint Set Union) and time complexity remains in each cases.
99% of people end up using DFS, however most for this question is BFS and reason is it has best space complexity!
Solution | Time Complexity | Space Complexity |
---|---|---|
DFS | O(N x M) | O(N x M) |
BFS | O(N x M) | O(min(N, M)) |
DSU | O(N x M) | O(N x M) |
int solve(vector>& grid) { int nr = grid.size(); if (!nr) return 0; int nc = grid[0].size(); int num_islands = 0; for (int r = 0; r < nr; ++r) { for (int c = 0; c < nc; ++c) { if (grid[r][c] == '1') { ++num_islands; grid[r][c] = '0'; // mark as visited queue > neighbors; neighbors.push({r, c}); while (!neighbors.empty()) { auto rc = neighbors.front(); neighbors.pop(); int row = rc.first, col = rc.second; if (row - 1 >= 0 && grid[row-1][col] == '1') { neighbors.push({row-1, col}); grid[row-1][col] = '0'; } if (row + 1 < nr && grid[row+1][col] == '1') { neighbors.push({row+1, col}); grid[row+1][col] = '0'; } if (col - 1 >= 0 && grid[row][col-1] == '1') { neighbors.push({row, col-1}); grid[row][col-1] = '0'; } if (col + 1 < nc && grid[row][col+1] == '1') { neighbors.push({row, col+1}); grid[row][col+1] = '0'; } } } } } return num_islands; }
This is not correct. The space complexity should also take into account the space used for input values (the grid in this case), and hence the space complexity will still be O(N * M).
When algorithms/solutions are being compared, it makes sense to compare them by space/time taken by the algorithm/solution itself. Thus, in interview setting when we say space complexity it defaults to improving Auxilary space complexity (helps compare solutions proposed by multiple people). Auxilary space complexity doesn't care about input taken by space.
I believe that when we talk about "Space Complexity", it means the auxiliary space plus the space for input. So it is technically not correct to mention "Space Complexity" as O(min(N, M)) because it is still O(N * M). That being said, it definitely gives a better impression if you clarify that the "Auxiliary space" taken by my algorithm is just O(min(N, M)) and compare it with the "Auxiliary space" taken by other algorithms.
It's always helpful to clarify when you speak & be technically appropriate, agreed 100%. The original idea still stands true. If you look at solutions provided to interviewers at Google, Microsoft, etc - they mean "auxiliary" but referred to it as Space complexity - re-enforcing bias in the industry.
Hey I have a few questions if you can answer them it would be great:
1. Which university or college are you from?
2. What was the ctc offered to you?