PIXELBANKv9.1.0
Menu

Number of Islands

Given a 2D grid of '1's (land) and '0's (water), count the number of islands. An island is surrounded by water and formed by connecting adjacent land cells horizontally/vertically.

Input: rows of comma-separated 1/0.

Example:

Input:
1,1,1,1,0
1,1,0,1,0
1,1,0,0,0
0,0,0,0,0
Output:
1
Reasoning:
  • The input is a 2D grid of '1's (land) and '0's (water), which can be visualized as:

1 1 1 1 0 1 1 0 1 0 1 1 0 0 0 0 0 0 0 0

* We identify the connected land cells (horizontally/vertically) to form islands. In this case, all the '1's are connected, forming a single large island.
* The number of islands is counted, which in this case is $1$ since all the land cells are part of the same island.
* The final output is the total count of islands, which is $\boxed{1}$.

Constraints:

  • 1 <= rows, cols <= 300
  • grid[i][j] is '0' or '1'
🔒

Editor locked

The code editor is locked for Pro problems. It is only available for free problems. Please upgrade to gain access to the code editor for all problems.

solution.py

Test Results

0/0
Run code to see test results.