-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathSolution.java
131 lines (122 loc) · 4.71 KB
/
Solution.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
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
// Solution 1
/*
class Solution {
private int idx = 2; // start from 2 to avoid conflicts
private Map<Integer, Integer> idxToArea = new HashMap<>(); // island idx to area
public int largestIsland(int[][] grid) {
for(int i = 0; i < grid.length; i++) {
for(int j = 0; j < grid[0].length; j++) {
if(grid[i][j] == 1) {
int area = getMaxArea(grid, i, j);
idxToArea.put(idx, area);
idx++;
}
}
}
int maxConnectedArea = 0;
// 如果当前存在island,将maxConnectedArea初始化为island的最大面积
if(idxToArea.size() != 0) {
maxConnectedArea = Collections.max(idxToArea.values());
}
// find water
for(int i = 0; i < grid.length; i++) {
for(int j = 0; j < grid[0].length; j++) {
// 只有当前坐标(i, j)为0的时候,尝试将当前坐标变为1,计算最大连通区域面积
if(grid[i][j] == 0) {
int area = getConnectedArea(grid, i, j);
maxConnectedArea = Math.max(area, maxConnectedArea);
}
}
}
return maxConnectedArea;
}
public int getMaxArea(int[][] grid, int row, int col) {
if(!inArea(grid, row, col)) return 0;
if(grid[row][col] != 1) return 0;
grid[row][col] = idx; // mark as idx
return 1
+ getMaxArea(grid, row - 1, col)
+ getMaxArea(grid, row + 1, col)
+ getMaxArea(grid, row, col - 1)
+ getMaxArea(grid, row, col + 1);
}
public boolean inArea(int[][] grid, int row, int col) {
return row >= 0 && row < grid.length &&
col >= 0 && col < grid[0].length;
}
public int getConnectedArea(int[][] grid, int row, int col) {
int res = 1;
Set<Integer> islandIdx = new HashSet<>();
int[][] directions = {{-1, 0}, {1, 0}, {0, -1}, {0, 1}};
for(int i = 0; i < 4; i++) {
int new_row = row + directions[i][0];
int new_col = col + directions[i][1];
if(inArea(grid, new_row, new_col) && grid[new_row][new_col] != 0) {
islandIdx.add(grid[new_row][new_col]);
}
}
for(int idx: islandIdx) {
res += idxToArea.get(idx);
}
return res;
}
}
*/
// Solution 2: 修改getConnectedArea函数的相关逻辑
class Solution {
private int idx = 2; // start from 2 to avoid conflicts
private Map<Integer, Integer> idxToArea = new HashMap<>(); // island idx to area
public int largestIsland(int[][] grid) {
for(int i = 0; i < grid.length; i++) {
for(int j = 0; j < grid[0].length; j++) {
if(grid[i][j] == 1) {
int area = getMaxArea(grid, i, j);
idxToArea.put(idx, area);
idx++;
}
}
}
int maxConnectedArea = 0;
// find water
for(int i = 0; i < grid.length; i++) {
for(int j = 0; j < grid[0].length; j++) {
int area = getConnectedArea(grid, i, j); // 获取当前坐标(i, j)的最大连通区域面积
maxConnectedArea = Math.max(area, maxConnectedArea);
}
}
return maxConnectedArea;
}
public int getMaxArea(int[][] grid, int row, int col) {
if(!inArea(grid, row, col)) return 0;
if(grid[row][col] != 1) return 0;
grid[row][col] = idx; // mark as idx
return 1
+ getMaxArea(grid, row - 1, col)
+ getMaxArea(grid, row + 1, col)
+ getMaxArea(grid, row, col - 1)
+ getMaxArea(grid, row, col + 1);
}
public boolean inArea(int[][] grid, int row, int col) {
return row >= 0 && row < grid.length &&
col >= 0 && col < grid[0].length;
}
public int getConnectedArea(int[][] grid, int row, int col) {
// 如果当前不为0,返回当前连通的最大区域面积
if(grid[row][col] != 0) return idxToArea.get(grid[row][col]);
// 否则,尝试将当前区域变为1,计算最大的连通面积
int res = 1;
Set<Integer> islandIdx = new HashSet<>();
int[][] directions = {{-1, 0}, {1, 0}, {0, -1}, {0, 1}};
for(int i = 0; i < 4; i++) {
int new_row = row + directions[i][0];
int new_col = col + directions[i][1];
if(inArea(grid, new_row, new_col) && grid[new_row][new_col] != 0) {
islandIdx.add(grid[new_row][new_col]);
}
}
for(int idx: islandIdx) {
res += idxToArea.get(idx);
}
return res;
}
}