Ctrl K

Sealed Islands

Medium
You get a chart chart, a rectangular grid where 0 is land and 1 is water. An island is a maximal group of land cells connected through their sides (up, down, left, right; diagonal cells are not connected). An island is sealed if water surrounds it on every side. An island that touches the edge of the chart is not sealed, since it might continue past the edge. Return the number of sealed islands.
Example 1
chart=[[1, 1, 1, 1], [1, 0, 1, 1], [1, 1, 0, 1], [1, 1, 1, 1]] → 2

The two land cells touch only at a corner, so they are two separate islands, and both are surrounded by water.

Example 2
chart=[[1, 1, 1], [1, 0, 0], [1, 1, 1]] → 0

The island reaches the right edge of the chart, so it does not count as sealed.

Example 3
chart=[[0]] → 0

The only cell lies on the edge of the chart.

Constraints
  • 1 ≤ len(chart), len(chart[0]) ≤ 100
  • chart[i][j] is 0 or 1
Have you seen this problem in a real interview?
Solved by0
Tried by0
Submissions0
Acceptance—
Topics
Similar problemsDrain the Landlocked Ponds MediumLand Regions on a Map MediumLargest Forest Patch MediumA Board Cut by Slashes MediumLandlocked Land Cells Medium
loading Python…
Input
chart=[[1, 1, 1, 1], [1, 0, 1, 1], [1, 1, 0, 1], [1, 1, 1, 1]]
Expected
2

Run checks the first 3 tests in your browser. Submit checks all 66 tests on the server, hidden ones included.