Location via proxy:   [ UP ]  
[Report a bug]   [Manage cookies]                
Skip to content

Commit f43f771

Browse files
solves island perimeter
1 parent 03eb352 commit f43f771

File tree

3 files changed

+54
-1
lines changed

3 files changed

+54
-1
lines changed

README.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -122,7 +122,7 @@
122122
| 455 | [Assign Cookies](https://leetcode.com/problems/assign-cookies) | Easy | [![Java](https://img.icons8.com/color/40/000000/java-coffee-cup-logo.png)](src/AssignCookies.java) [![Python](https://img.icons8.com/color/35/000000/python.png)](python/assign_cookies.py)|
123123
| 459 | [Repeated Substring Pattern](https://leetcode.com/problems/repeated-substring-pattern) | Easy | [![Java](https://img.icons8.com/color/40/000000/java-coffee-cup-logo.png)](src/RepeatedSubstringPattern.java) [![Python](https://img.icons8.com/color/35/000000/python.png)](python/repeated_substring_pattern.py) |
124124
| 461 | [Hamming Distance](https://leetcode.com/problems/hamming-distance) | Easy | [![Java](https://img.icons8.com/color/40/000000/java-coffee-cup-logo.png)](src/HammingDistance.java) [![Python](https://img.icons8.com/color/35/000000/python.png)](python/hamming_distance.py) |
125-
| 463 | [Island Perimeter](https://leetcode.com/problems/island-perimeter) | Easy | |
125+
| 463 | [Island Perimeter](https://leetcode.com/problems/island-perimeter) | Easy | [![Java](https://img.icons8.com/color/40/000000/java-coffee-cup-logo.png)](src/IslandPerimeter.java) [![Python](https://img.icons8.com/color/35/000000/python.png)](python/island_perimeter.py) |
126126
| 475 | [Heaters](https://leetcode.com/problems/heaters) | Easy | |
127127
| 476 | [Number Complement](https://leetcode.com/problems/number-complement) | Easy | |
128128
| 482 | [License Key Formatting](https://leetcode.com/problems/license-key-formatting) | Easy | |

python/island_perimeter.py

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
from typing import List
2+
3+
4+
class Solution:
5+
def islandPerimeter(self, grid: List[List[int]]) -> int:
6+
perimeter, rows, columns = 0, len(grid), len(grid[0])
7+
for row in range(rows):
8+
for column in range(columns):
9+
if grid[row][column] == 1:
10+
perimeter += 4 - self.surroundedBy(grid, row, column)
11+
return perimeter
12+
13+
def surroundedBy(self, grid: List[List[int]], row: int, column: int) -> int:
14+
surrounded = 0
15+
if row - 1 >= 0 and grid[row - 1][column] == 1:
16+
surrounded += 1
17+
if column + 1 < len(grid[0]) and grid[row][column + 1] == 1:
18+
surrounded += 1
19+
if row + 1 < len(grid) and grid[row + 1][column] == 1:
20+
surrounded += 1
21+
if column - 1 >= 0 and grid[row][column - 1] == 1:
22+
surrounded += 1
23+
return surrounded

src/IslandPerimeter.java

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
public class IslandPerimeter {
2+
public int islandPerimeter(int[][] grid) {
3+
int perimeter = 0;
4+
for (int row = 0 ; row < grid.length ; row++) {
5+
for (int column = 0 ; column < grid[row].length ; column++) {
6+
if (grid[row][column] == 1) {
7+
perimeter += 4 - surroundedBy(grid, row, column);
8+
}
9+
}
10+
}
11+
return perimeter;
12+
}
13+
14+
private static int surroundedBy(int[][] grid, int row, int column) {
15+
int surrounded = 0;
16+
if (row - 1 >= 0 && grid[row - 1][column] == 1) {
17+
surrounded++;
18+
}
19+
if (column + 1 < grid[0].length && grid[row][column + 1] == 1) {
20+
surrounded++;
21+
}
22+
if (row + 1 < grid.length && grid[row + 1][column] == 1) {
23+
surrounded++;
24+
}
25+
if (column - 1 >= 0 && grid[row][column - 1] == 1) {
26+
surrounded++;
27+
}
28+
return surrounded;
29+
}
30+
}

0 commit comments

Comments
 (0)