-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy path463.linningmii.py
57 lines (43 loc) · 1.19 KB
/
463.linningmii.py
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
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
class Solution:
def __init__(self):
self.grid = []
def islandPerimeter(self, grid):
"""
:type grid: List[List[int]]
:rtype: int
"""
self.grid = grid
edge_count = 0
for y in range(len(grid)):
for x in range(len(grid[y])):
edge_count += self.count_edge([x, y])
return edge_count
def count_edge(self, coord):
"""
:type coord: [int, int]
:rtype: int
"""
x = coord[0]
y = coord[1]
if self.grid[y][x] == 0:
return 0
else:
edge_count = 4
if x - 1 >= 0 and self.grid[y][x - 1] == 1:
edge_count -= 1
if x + 1 < len(self.grid[y]) and self.grid[y][x + 1] == 1:
edge_count -= 1
if y - 1 >= 0 and self.grid[y - 1][x] == 1:
edge_count -= 1
if y + 1 < len(self.grid) and self.grid[y + 1][x] == 1:
edge_count -= 1
return edge_count
solution = Solution()
print(solution.islandPerimeter(
[
[0, 1, 0, 0],
[1, 1, 1, 0],
[0, 1, 0, 0],
[1, 1, 0, 0]
]
))