-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy path463_IslandPerimeter.py
More file actions
27 lines (22 loc) · 879 Bytes
/
463_IslandPerimeter.py
File metadata and controls
27 lines (22 loc) · 879 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
class Solution:
def sum_adjacent(self, grid, i, j):
adjacent = (i + 1, j), (i - 1, j), (i, j + 1), (i, j - 1),
res = 0
for x, y in adjacent:
if x < 0 or y < 0 or x == len(grid) or y == len(grid[0]) or grid[x][y] == 0:
res += 1
return res
def islandPerimeter(self, grid: 'List[List[int]]') -> 'int':
count = 0
for i in range(len(grid)):
for j in range(len(grid[0])):
if grid[i][j] == 1:
count += self.sum_adjacent(grid, i, j)
return count
def main():
input = [[0, 1, 0, 0], [1, 1, 1, 0], [0, 1, 0, 0], [1, 1, 0, 0]]
# Output: 16er(input))
# Explanation: The perimeter is the 16 yellow stripes in the image below:
print(Solution().islandPerimeter(input))
if __name__ == '__main__':
main()