-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy path463. Island Perimeter.py
More file actions
29 lines (22 loc) · 862 Bytes
/
463. Island Perimeter.py
File metadata and controls
29 lines (22 loc) · 862 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
class Solution:
def islandPerimeter(self, grid: List[List[int]]) -> int:
perim = 0
h, w = len(grid), len(grid[0])
# Iterate through each cell
for row in range(0, h):
for col in range(0, w):
# Is this a land cell?
if grid[row][col] == 1:
# Top edge
if row == 0 or grid[row - 1][col] == 0:
perim += 1
# Bottom edge
if row == h - 1 or grid[row + 1][col] == 0:
perim += 1
# Left edge
if col == 0 or grid[row][col - 1] == 0:
perim += 1
# Right edge
if col == w - 1 or grid[row][col + 1] == 0:
perim += 1
return perim