forked from neetcode-gh/leetcode
-
Notifications
You must be signed in to change notification settings - Fork 0
/
0463-island-perimeter.java
33 lines (29 loc) · 1.13 KB
/
0463-island-perimeter.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
31
32
33
class Solution {
class RecursiveBiFunction<A, B, C> {
BiFunction<A, B, C> func;
}
public int islandPerimeter(int[][] _grid) {
final int[][] grid = _grid;
final Set<Integer> visit = new HashSet<>();
final RecursiveBiFunction<Integer, Integer, Integer> dfs = new RecursiveBiFunction();
dfs.func = (i, j) -> {
if(i >= grid.length || j >= grid[0].length || i < 0 || j < 0 || grid[i][j] == 0)
return 1;
//convert 2D-Coordinate to 1D-Coordinate
int flatCoord = i*grid[0].length + j;
if(visit.contains(flatCoord))
return 0;
visit.add(flatCoord);
int perim = dfs.func.apply(i, j + 1);
perim += dfs.func.apply(i + 1, j);
perim += dfs.func.apply(i, j - 1);
perim += dfs.func.apply(i - 1, j);
return perim;
};
for(int i = 0; i < grid.length; i++)
for(int j = 0; j < grid[0].length; j++)
if(grid[i][j] != 0)
return dfs.func.apply(i, j);
return -1;
}
}