-
Notifications
You must be signed in to change notification settings - Fork 16
/
Copy pathIslandPerimeter.java
30 lines (29 loc) · 965 Bytes
/
IslandPerimeter.java
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
public class IslandPerimeter {
public int islandPerimeter(int[][] grid) {
int perimeter = 0;
for (int row = 0 ; row < grid.length ; row++) {
for (int column = 0 ; column < grid[row].length ; column++) {
if (grid[row][column] == 1) {
perimeter += 4 - surroundedBy(grid, row, column);
}
}
}
return perimeter;
}
private static int surroundedBy(int[][] grid, int row, int column) {
int surrounded = 0;
if (row - 1 >= 0 && grid[row - 1][column] == 1) {
surrounded++;
}
if (column + 1 < grid[0].length && grid[row][column + 1] == 1) {
surrounded++;
}
if (row + 1 < grid.length && grid[row + 1][column] == 1) {
surrounded++;
}
if (column - 1 >= 0 && grid[row][column - 1] == 1) {
surrounded++;
}
return surrounded;
}
}