Skip to main content
All Visualizers
#LC 200•Matrix•Medium

Number of Islands

Count the connected groups of land cells in a grid.

Understand the problem

Count the connected groups of land cells in a grid.

Example 1

Input: {"grid": [["1", "1", "0"], ["1", "0", "0"], ["0", "0", "1"]]}

Output: 2

Follow the trace to see how the algorithm reaches this result.

Constraints

  • Use the input format shown in the example.
Think first

What information should you keep so you can solve the next step efficiently?

Pattern tags:Grid DFSConnected components
Time: O(m * n)Space: O(m * n)
Step 1 of 5 • Use Left/Right arrows or Space to control