O43

Number of Islands

Mid / Senior Engineer
Target Time: 25 - 35 Minutes
Type: NUMERIC

Problem Statement & Objective

Given an m x n 2D binary grid grid which represents a map of '1's (land) and '0's (water), return the number of islands.

Function Signature (Python)

num_islands(grid: list[list[str]]) -> int

Input & Output Contract

Output Rule:Ada sayısı tam sayısı.

System Constraints

m == izgara.length, n == izgara[i].length
1 <= m, n <= 300
izgara[i][j] ∈ {'0', '1'}

Example Execution Walkthrough

izgara = [
  ['1','1','0','0','0'],
  ['1','1','0','0','0'],
  ['0','0','1','0','0'],
  ['0','0','0','1','1']
] -> 3 ada

Example Test Scenarios (3)

Example 1: Tek Büyük Ada
Input:[[["1","1","1","1","0"],["1","1","0","1","0"],["1","1","0","0","0"],["0","0","0","0","0"]]]
Expected Output:1
Example 2: 3 Ada
Input:[[["1","1","0","0","0"],["1","1","0","0","0"],["0","0","1","0","0"],["0","0","0","1","1"]]]
Expected Output:3
Example 3: Yalnız Su
Input:[[["0"]]]
Expected Output:0
Loading Python Editor...
Input Parameters
[
  [
    [
      "1",
      "1",
      "1",
      "1",
      "0"
    ],
    [
      "1",
      "1",
      "0",
      "1",
      "0"
    ],
    [
      "1",
      "1",
      "0",
      "0",
      "0"
    ],
    [
      "0",
      "0",
      "0",
      "0",
      "0"
    ]
  ]
]
Expected Output
1