soapyigu/LeetCode-Swift

Number of Islands complexity

stepanhruda opened this issue · 2 comments

Currently complexity: O((n^2)!)

  1. Tiles are switched to 0 once visited, so they cannot be visited again. Although the search can look into 4 directions for each tile, it doesn't keep going.
  2. The instructions don't say that the board is a square (although it is in the example).

Suggested complexity: O(mn)

Hi,

For 1, I did it to track if a tile is visited or not, and that is necessary for DFS;
For 2, you are right, O(mn) is more accurate.

Thank you for your suggestions,
Yi

Oh I agree with the approach, just meant to back up why I thought the complexity was lower. I see you already merged a fix.