Home
Log in
DescriptionSubmissionsSolution
DescriptionSubmissionsSolution
Loading...

Log in to run or submit

grid =

Codey

Practise coding problems, test your solutions and track your progress.

Explore

  • Problems
  • About
  • Contact

Legal

  • Privacy Policy
  • Terms of Use

© 2026 Codey. Personal learning project.

Solution
Updated: 2026-02-23

Idea

Count connected components of land cells.

Approach

Scan grid; when you find a '1', increment answer and DFS/BFS to flood-fill and mark the entire island as water.

Why it works

Each island is one connected component in 4-direction adjacency; flood-fill visits it exactly once.

Code
Loading...
Complexity
Time: O(mn)
Space: O(mn) worst-case recursion/queue
Solution
Updated: 2026-02-23

Idea

Count connected components of land cells.

Approach

Scan grid; when you find a '1', increment answer and DFS/BFS to flood-fill and mark the entire island as water.

Why it works

Each island is one connected component in 4-direction adjacency; flood-fill visits it exactly once.

Code
Loading...
Complexity
Time: O(mn)
Space: O(mn) worst-case recursion/queue