-
Notifications
You must be signed in to change notification settings - Fork 16
Expand file tree
/
Copy pathMakingALargeIsland.java
More file actions
52 lines (41 loc) · 1.32 KB
/
MakingALargeIsland.java
File metadata and controls
52 lines (41 loc) · 1.32 KB
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
class Solution {
int[] dr = new int[]{-1,0,1,0};
int[] dc = new int[]{0,-1,0,1};
public int largestIsland(int[][] grid) {
int N = grid.length;
int ans = 0;
boolean hasZero = false;
for(int r=0;r<N;r++) {
for(int c=0;c<N;c++) {
if(grid[r][c]==0) {
hasZero = true;
grid[r][c]=1;
ans = (int)Math.max(ans,check(grid,r,c));
grid[r][c]=0;
}
}
}
return hasZero?ans:N*N;
}
public int check(int[][] grid, int r, int c){
int N = grid.length;
Stack<Integer> stack = new Stack<>();
Set<Integer> set = new HashSet<>();
stack.push(r*N+c);
set.add(r*N+c);
while(!stack.isEmpty()) {
int temp = stack.pop();
int x = temp/N;
int y = temp%N;
for(int k=0;k<4;k++){
int nr = x + dr[k];
int nc = y + dc[k];
if(!set.contains(nr*N+nc) && nr>=0 && nr<N && nc<N && nc>=0 && grid[nr][nc]==1) {
stack.push(nr*N+nc);
set.add(nr*N+nc);
}
}
}
return set.size();
}
}