-
-
Notifications
You must be signed in to change notification settings - Fork 48
Expand file tree
/
Copy path0463-island-perimeter.py
More file actions
30 lines (26 loc) · 1009 Bytes
/
0463-island-perimeter.py
File metadata and controls
30 lines (26 loc) · 1009 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
30
# time complexity: O(n*m)
# space complexity: O(1)
from typing import List
class Solution:
def islandPerimeter(self, grid: List[List[int]]) -> int:
edgeCount = 0
ROW = len(grid)
COL = len(grid[0])
for r in range(ROW):
for c in range(COL):
if grid[r][c] == 1:
if (c > 0 and grid[r][c - 1] == 0) or c == 0:
edgeCount += 1
if (r > 0 and grid[r - 1][c] == 0) or r == 0:
edgeCount += 1
if (c < COL - 1 and grid[r][c + 1] == 0) or c == COL - 1:
edgeCount += 1
if (r < ROW - 1 and grid[r + 1][c] == 0) or r == ROW - 1:
edgeCount += 1
return edgeCount
grid = [[0, 1, 0, 0], [1, 1, 1, 0], [0, 1, 0, 0], [1, 1, 0, 0]]
print(Solution().islandPerimeter(grid))
grid = [[1]]
print(Solution().islandPerimeter(grid))
grid = [[1, 0]]
print(Solution().islandPerimeter(grid))