-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathIslandPerimeter.java
More file actions
39 lines (33 loc) · 893 Bytes
/
IslandPerimeter.java
File metadata and controls
39 lines (33 loc) · 893 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
31
32
33
34
35
36
37
38
39
package Array;
/**
* Author - archit.s
* Date - 22/08/18
* Time - 12:47 PM
*/
public class IslandPerimeter {
public int islandPerimeter(int[][] grid) {
int row = grid.length;
int col = grid[0].length;
int sum = 0;
for(int i=0;i<row;i++){
for(int j=0;j<col;j++){
if(grid[i][j] == 1){
sum+=4;
if(i-1>=0 && grid[i-1][j] == 1){
sum -= 1;
}
if(i+1<row && grid[i+1][j] == 1){
sum -= 1;
}
if(j-1>=0 && grid[i][j-1] == 1){
sum -= 1;
}
if(j+1<col && grid[i][j+1] == 1){
sum -= 1;
}
}
}
}
return sum;
}
}