-
Notifications
You must be signed in to change notification settings - Fork 0
/
463. Island Perimeter.py
33 lines (33 loc) · 1.37 KB
/
463. Island Perimeter.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
class Solution:
def islandPerimeter(self, grid: List[List[int]]) -> int:
#soln 1 - Leetcode, better count:
ans = 0
for r, row in enumerate(grid):
for c, val in enumerate(row):
if val:
ans += 4
if r > 0 and grid[r-1][c]:
ans -= 2
if c > 0 and grid[r][c-1]:
ans -= 2
return ans
#soln 0 - first attempt, time O(MN)
# m, n = len(grid), len(grid[0])
# ans = 0
# for r, row in enumerate(grid):
# for c, val in enumerate(row):
# if val:
# if r == 0:
# ans += 1
# else: #r > 0
# if grid[r-1][c] == 0:
# ans += 1
# if r == m-1:
# ans += 1
# else: #r < m-1
# if grid[r+1][c] == 0:
# ans += 1