Loading
Loading
A cartographer is measuring coastline on a square-tile map. Each tile is land (1) or water (0). Every tile has side length 1, and anything beyond the edge of the map is open sea.
A unit of coastline is any tile side where a land tile meets water or the edge of the map. Return the total coastline of map, summed over every piece of land.
map = [[0,1,0,0],[1,1,1,0],[0,1,0,0],[1,1,0,0]]7 land tiles7 × 4 = 28 sides in total6 touching pairseach shared side hides 2 sides28 − 12 = 16coastline that remains exposed16Sign in to keep a note here. It is free, and nobody else can see it.
Sign inExample 1
| 0 | 0 | 1 | 0 | 0 |
|---|---|---|---|---|
| 1 | 1 | 1 | 1 | 0 |
| 2 | 0 | 1 | 0 | 0 |
| 3 | 1 | 1 | 0 | 0 |
Sign in to open hints.
Struggling is where the learning happens. Open this once you have genuinely tried, or once the hints have run out.
O(rows × cols) time · O(1) space
Input. Run your code to watch it work on this input, one line at a time.
| 0 | 0 | 1 | 0 | 0 |
|---|---|---|---|---|
| 1 | 1 | 1 | 1 | 0 |
| 2 | 0 | 1 | 0 | 0 |
| 3 | 1 | 1 | 0 | 0 |
Running code is turned off on this deployment. You can still read the problem, write code, use hints and the visualizer.
Tab moves focus out of the editor. Press Ctrl+M to let Tab indent instead.
No results yet
Run your code against the sample tests, or submit to run the full suite.
Example 2
| 0 | 1 | 1 |
|---|---|---|
| 1 | 1 | 1 |
Example 3
| 0 | 1 | 0 | 1 |
|---|