Ctrl K

Farthest From Any Shore

Medium
A sea chart is a square grid chart of size n × n: 1 is a land cell and 0 is a water cell. The distance between cells (r1, c1) and (r2, c2) is |r1 - r2| + |c1 - c2|. A captain is looking for the most secluded spot at sea: a water cell whose distance to the nearest land cell is as large as possible. Return that distance. If the chart has no land at all, or no water at all, return -1.
Example 1
chart=[[0, 0, 1], [0, 0, 0], [1, 0, 0]] → 2

Cells (0, 0) and (2, 2) are 2 steps from the nearest island, and no cell is farther.

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

The only island sits in the bottom-right corner; the top-left cell is 3 + 3 = 6 away from it.

Example 3
chart=[[1, 1], [1, 1]] → -1

There is no water at all, so the answer is -1.

Constraints
  • 1 ≤ n ≤ 200
  • chart is a square n × n grid
  • chart[i][j] is 0 or 1
Have you seen this problem in a real interview?
Solved by0
Tried by0
Submissions0
Acceptance—
Topics
Similar problemsSteps to the Nearest Zero MediumBlock the Only Route MediumStones on a 3 × 3 Board MediumLongest Uphill Walk HardSix-Cell Tile Slider Hard
loading Python…
Input
chart=[[0, 0, 1], [0, 0, 0], [1, 0, 0]]
Expected
2

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