989. Number of Islands (DFS)

EasyGraphsGridDFSGraph

Given a 2D grid of 1s (land) and 0s (water), count the number of islands. An island is a maximal group of 1s connected horizontally or vertically. The input is JSON {grid}.

Input: JSON {grid}.

Output: Integer — the number of islands.

Examples

Example 1
Input: {"grid":[[1,1,0,0],[1,0,0,1],[0,0,1,1]]}
Output: 2
Explanation: Two separate land masses.
Example 2
Input: {"grid":[[0]]}
Output: 0
Explanation: All water.
Example 3
Input: {"grid":[[1]]}
Output: 1
Explanation: One land cell.

Constraints

Asked by

AmazonBloombergAppleOracleAdobeMicrosoft
Solve this problem in the editor →