-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathSolution463.py
26 lines (23 loc) · 931 Bytes
/
Solution463.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
class Solution(object):
def islandPerimeter(self, grid):
"""
:type grid: List[List[int]]
:rtype: int
"""
perimeter = 0
rows, cols = len(grid), len(grid[0])
for i in range(rows):
for j in range(cols):
if grid[i][j] == 1:
perimeter += 4 # Start with the assumption of 4 sides for each land cell
# Check each adjacent cell, subtracting 1 from perimeter for each adjacent land cell
if i > 0 and grid[i - 1][j] == 1:
perimeter -= 2
if j > 0 and grid[i][j - 1] == 1:
perimeter -= 2
return perimeter
# Test cases
sol = Solution()
print(sol.islandPerimeter([[0,1,0,0],[1,1,1,0],[0,1,0,0],[1,1,0,0]])) # Output: 16
print(sol.islandPerimeter([[1]])) # Output: 4
print(sol.islandPerimeter([[1,0]])) # Output: 4