-
Notifications
You must be signed in to change notification settings - Fork 43
/
island_perimeter.py
207 lines (169 loc) · 6.32 KB
/
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
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
"""
463. Island Perimeter
Easy
You are given row x col grid representing a map where grid[i][j] = 1 represents land and grid[i][j] = 0 represents water.
Grid cells are connected horizontally/vertically (not diagonally). The grid is completely surrounded by water, and there is exactly one island (i.e., one or more connected land cells).
The island doesn't have "lakes", meaning the water inside isn't connected to the water around the island. One cell is a square with side length 1. The grid is rectangular, width and height don't exceed 100. Determine the perimeter of the island.
Example 1:
Input: grid = [[0,1,0,0],[1,1,1,0],[0,1,0,0],[1,1,0,0]]
Output: 16
Explanation: The perimeter is the 16 yellow stripes in the image above.
Example 2:
Input: grid = [[1]]
Output: 4
Example 3:
Input: grid = [[1,0]]
Output: 4
Constraints:
row == grid.length
col == grid[i].length
1 <= row, col <= 100
grid[i][j] is 0 or 1.
There is exactly one island in grid.
"""
# V0
class Solution:
def islandPerimeter(self, grid):
"""
:type grid: List[List[int]]
:rtype: int
"""
M, N = len(grid), len(grid[0])
counts = 0
neighbors = 0
for i in range(M):
for j in range(N):
if grid[i][j] == 1:
counts += 1
if i < M - 1 and grid[i + 1][j] == 1:
neighbors += 1
if j < N - 1 and grid[i][j + 1] == 1:
neighbors += 1
return 4 * counts - 2 * neighbors
# V0'
# IDEA : SIMPLE COUNTING
# IDEA :
# -> Go through every cell on the grid and whenever you are at cell 1 (land cell), look for surrounding (UP, RIGHT, DOWN, LEFT) cells. A land cell without any surrounding land cell will have a perimeter of 4. Subtract 1 for each surrounding land cell.
# -> When you are at cell 0 (water cell), you don't need to do anything. Just proceed to another cell.
class Solution:
def islandPerimeter(self, grid):
rows = len(grid)
cols = len(grid[0])
result = 0
for r in range(rows):
for c in range(cols):
if grid[r][c] == 1:
if r == 0:
up = 0
else:
up = grid[r-1][c]
if c == 0:
left = 0
else:
left = grid[r][c-1]
if r == rows-1:
down = 0
else:
down = grid[r+1][c]
if c == cols-1:
right = 0
else:
right = grid[r][c+1]
result += 4-(up+left+right+down)
return result
# V1
# IDEA : SIMPLE COUNTING
# -> Go through every cell on the grid and whenever you are at cell 1 (land cell), look for surrounding (UP, RIGHT, DOWN, LEFT) cells. A land cell without any surrounding land cell will have a perimeter of 4. Subtract 1 for each surrounding land cell.
# -> When you are at cell 0 (water cell), you don't need to do anything. Just proceed to another cell.
# https://leetcode.com/problems/island-perimeter/solution/
class Solution:
def islandPerimeter(self, grid: List[List[int]]) -> int:
rows = len(grid)
cols = len(grid[0])
result = 0
for r in range(rows):
for c in range(cols):
if grid[r][c] == 1:
if r == 0:
up = 0
else:
up = grid[r-1][c]
if c == 0:
left = 0
else:
left = grid[r][c-1]
if r == rows-1:
down = 0
else:
down = grid[r+1][c]
if c == cols-1:
right = 0
else:
right = grid[r][c+1]
result += 4-(up+left+right+down)
return result
# V1'
# IDEA : BETTER COUNTING
# https://leetcode.com/problems/island-perimeter/solution/
class Solution:
def islandPerimeter(self, grid: List[List[int]]) -> int:
rows = len(grid)
cols = len(grid[0])
result = 0
for r in range(rows):
for c in range(cols):
if grid[r][c] == 1:
result += 4
if r > 0 and grid[r-1][c] == 1:
result -= 2
if c > 0 and grid[r][c-1] == 1:
result -= 2
return result
# V1''
# https://blog.csdn.net/fuxuemingzhu/article/details/83868905
class Solution:
def islandPerimeter(self, grid):
"""
:type grid: List[List[int]]
:rtype: int
"""
M, N = len(grid), len(grid[0])
counts = 0
neighbors = 0
for i in range(M):
for j in range(N):
if grid[i][j] == 1:
counts += 1
if i < M - 1:
if grid[i + 1][j] == 1:
neighbors += 1
if j < N - 1:
if grid[i][j + 1] == 1:
neighbors += 1
return 4 * counts - 2 * neighbors
# V1'''
# https://www.jiuzhang.com/solution/island-perimeter/#tag-highlight-lang-python
class Solution:
"""
@param grid: a 2D array
@return: the perimeter of the island
"""
def islandPerimeter(self, grid):
# Write your code here
rowN = len(grid)
colN = len(grid[0])
topBottom = [0] * colN
grid = [topBottom] + grid + [topBottom]
grid = [[0] + x + [0] for x in grid]
perimeter = 0
for i in range(rowN + 2):
for j in range(colN + 2):
elem = grid[i][j]
if elem == 1:
up = 1 - grid[i-1][j]
down = 1 - grid[i+1][j]
left = 1 - grid[i][j-1]
right = 1 - grid[i][j+1]
perimeter += (up + down + left + right)
return perimeter
# V2