37
37
38
38
## Solutions
39
39
40
- Union find.
41
-
42
40
<!-- tabs:start -->
43
41
44
42
### ** Python3**
45
43
46
44
``` python
47
45
class Solution :
48
46
def pondSizes (self , land : List[List[int ]]) -> List[int ]:
47
+ def dfs (i : int , j : int ) -> int :
48
+ res = 1
49
+ land[i][j] = 1
50
+ for x in range (i - 1 , i + 2 ):
51
+ for y in range (j - 1 , j + 2 ):
52
+ if 0 <= x < m and 0 <= y < n and land[x][y] == 0 :
53
+ res += dfs(x, y)
54
+ return res
55
+
49
56
m, n = len (land), len (land[0 ])
50
- p = list (range (m * n))
51
- size = [1 ] * (m * n)
52
-
53
- def find (x ):
54
- if p[x] != x:
55
- p[x] = find(p[x])
56
- return p[x]
57
-
58
- def union (a , b ):
59
- pa, pb = find(a), find(b)
60
- if pa == pb:
61
- return
62
- size[pb] += size[pa]
63
- p[pa] = pb
64
-
65
- for i in range (m):
66
- for j in range (n):
67
- if land[i][j] != 0 :
68
- continue
69
- idx = i * n + j
70
- if i < m - 1 and land[i + 1 ][j] == 0 :
71
- union(idx, (i + 1 ) * n + j)
72
- if j < n - 1 and land[i][j + 1 ] == 0 :
73
- union(idx, i * n + j + 1 )
74
- if i < m - 1 and j < n - 1 and land[i + 1 ][j + 1 ] == 0 :
75
- union(idx, (i + 1 ) * n + j + 1 )
76
- if i < m - 1 and j > 0 and land[i + 1 ][j - 1 ] == 0 :
77
- union(idx, (i + 1 ) * n + j - 1 )
78
-
79
- s = set ()
80
- res = []
81
- for i in range (m * n):
82
- if land[i // n][i % n] != 0 :
83
- continue
84
- root = find(i)
85
- if root not in s:
86
- s.add(root)
87
- res.append(size[root])
88
- res.sort()
89
- return res
57
+ return sorted (dfs(i, j) for i in range (m) for j in range (n) if land[i][j] == 0 )
90
58
```
91
59
92
60
### ** Java**
93
61
94
62
``` java
95
63
class Solution {
96
- private int [] p;
97
- private int [] size;
64
+ private int m;
65
+ private int n;
66
+ private int [][] land;
98
67
99
68
public int [] pondSizes (int [][] land ) {
100
- int m = land. length, n = land[0 ]. length;
101
- p = new int [m * n];
102
- size = new int [m * n];
103
- for (int i = 0 ; i < p. length; ++ i) {
104
- p[i] = i;
105
- size[i] = 1 ;
106
- }
69
+ m = land. length;
70
+ n = land[0 ]. length;
71
+ this . land = land;
72
+ List<Integer > ans = new ArrayList<> ();
107
73
for (int i = 0 ; i < m; ++ i) {
108
74
for (int j = 0 ; j < n; ++ j) {
109
- if (land[i][j] != 0 ) {
110
- continue ;
111
- }
112
- int idx = i * n + j;
113
- if (i < m - 1 && land[i + 1 ][j] == 0 ) {
114
- union(idx, (i + 1 ) * n + j);
115
- }
116
- if (j < n - 1 && land[i][j + 1 ] == 0 ) {
117
- union(idx, i * n + j + 1 );
118
- }
119
- if (i < m - 1 && j < n - 1 && land[i + 1 ][j + 1 ] == 0 ) {
120
- union(idx, (i + 1 ) * n + j + 1 );
121
- }
122
- if (i < m - 1 && j > 0 && land[i + 1 ][j - 1 ] == 0 ) {
123
- union(idx, (i + 1 ) * n + j - 1 );
75
+ if (land[i][j] == 0 ) {
76
+ ans. add(dfs(i, j));
124
77
}
125
78
}
126
79
}
127
- Set<Integer > s = new HashSet<> ();
128
- List<Integer > t = new ArrayList<> ();
129
- for (int i = 0 ; i < m * n; ++ i) {
130
- if (land[i / n][i % n] != 0 ) {
131
- continue ;
132
- }
133
- int root = find(i);
134
- if (! s. contains(root)) {
135
- s. add(root);
136
- t. add(size[root]);
137
- }
138
- }
139
- Collections . sort(t);
140
- int [] res = new int [t. size()];
141
- for (int i = 0 ; i < res. length; ++ i) {
142
- res[i] = t. get(i);
143
- }
144
- return res;
80
+ return ans. stream(). sorted(). mapToInt(Integer :: valueOf). toArray();
145
81
}
146
82
147
- private int find (int x ) {
148
- if (p[x] != x) {
149
- p[x] = find(p[x]);
150
- }
151
- return p[x];
152
- }
153
-
154
- private void union (int a , int b ) {
155
- int pa = find(a), pb = find(b);
156
- if (pa == pb) {
157
- return ;
83
+ private int dfs (int i , int j ) {
84
+ int res = 1 ;
85
+ land[i][j] = 1 ;
86
+ for (int x = i - 1 ; x <= i + 1 ; ++ x) {
87
+ for (int y = j - 1 ; y <= j + 1 ; ++ y) {
88
+ if (x >= 0 && x < m && y >= 0 && y < n && land[x][y] == 0 ) {
89
+ res += dfs(x, y);
90
+ }
91
+ }
158
92
}
159
- size[pb] += size[pa];
160
- p[pa] = pb;
93
+ return res;
161
94
}
162
95
}
163
96
```
@@ -167,117 +100,92 @@ class Solution {
167
100
``` cpp
168
101
class Solution {
169
102
public:
170
- vector<int > p;
171
- vector<int > size;
172
-
173
103
vector<int > pondSizes(vector<vector<int >>& land) {
174
104
int m = land.size(), n = land[ 0] .size();
175
- for (int i = 0; i < m * n; ++i) {
176
- p.push_back(i);
177
- size.push_back(1);
178
- }
105
+ function<int(int, int)> dfs = [ &] (int i, int j) -> int {
106
+ int res = 1;
107
+ land[ i] [ j ] = 1;
108
+ for (int x = i - 1; x <= i + 1; ++x) {
109
+ for (int y = j - 1; y <= j + 1; ++y) {
110
+ if (x >= 0 && x < m && y >= 0 && y < n && land[ x] [ y ] == 0) {
111
+ res += dfs(x, y);
112
+ }
113
+ }
114
+ }
115
+ return res;
116
+ };
117
+ vector<int > ans;
179
118
for (int i = 0; i < m; ++i) {
180
119
for (int j = 0; j < n; ++j) {
181
- if (land[i][j] != 0) continue;
182
- int idx = i * n + j;
183
- if (i < m - 1 && land[i + 1][j] == 0) unite(idx, (i + 1) * n + j);
184
- if (j < n - 1 && land[i][j + 1] == 0) unite(idx, i * n + j + 1);
185
- if (i < m - 1 && j < n - 1 && land[i + 1][j + 1] == 0) unite(idx, (i + 1) * n + j + 1);
186
- if (i < m - 1 && j > 0 && land[i + 1][j - 1] == 0) unite(idx, (i + 1) * n + j - 1);
187
- }
188
- }
189
- unordered_set<int> s;
190
- vector<int> res;
191
- for (int i = 0; i < m * n; ++i) {
192
- if (land[i / n][i % n] != 0) continue;
193
- int root = find(i);
194
- if (s.find(root) == s.end()) {
195
- s.insert(root);
196
- res.push_back(size[root]);
120
+ if (land[ i] [ j ] == 0) {
121
+ ans.push_back(dfs(i, j));
122
+ }
197
123
}
198
124
}
199
- sort (res.begin(), res.end());
200
- return res;
201
- }
202
-
203
- int find(int x) {
204
- if (p[x] != x) p[x] = find(p[x]);
205
- return p[x];
206
- }
207
-
208
- void unite(int a, int b) {
209
- int pa = find(a), pb = find(b);
210
- if (pa == pb) return;
211
- size[pb] += size[pa];
212
- p[pa] = pb;
125
+ sort(ans.begin(), ans.end());
126
+ return ans;
213
127
}
214
128
};
215
129
```
216
130
217
131
### **Go**
218
132
219
133
```go
220
- var p []int
221
- var size []int
222
-
223
- func pondSizes (land [][]int ) []int {
134
+ func pondSizes(land [][]int) (ans []int) {
224
135
m, n := len(land), len(land[0])
225
- p = make ([]int , m*n)
226
- size = make ([]int , m*n)
227
- for i := 0 ; i < m*n; i++ {
228
- p[i] = i
229
- size[i] = 1
230
- }
231
- for i := 0 ; i < m; i++ {
232
- for j := 0 ; j < n; j++ {
233
- if land[i][j] != 0 {
234
- continue
235
- }
236
- idx := i*n + j
237
- if i < m-1 && land[i+1 ][j] == 0 {
238
- union (idx, (i+1 )*n+j)
239
- }
240
- if j < n-1 && land[i][j+1 ] == 0 {
241
- union (idx, i*n+j+1 )
242
- }
243
- if i < m-1 && j < n-1 && land[i+1 ][j+1 ] == 0 {
244
- union (idx, (i+1 )*n+j+1 )
245
- }
246
- if i < m-1 && j > 0 && land[i+1 ][j-1 ] == 0 {
247
- union (idx, (i+1 )*n+j-1 )
136
+ var dfs func(i, j int) int
137
+ dfs = func(i, j int) int {
138
+ res := 1
139
+ land[i][j] = 1
140
+ for x := i - 1; x <= i+1; x++ {
141
+ for y := j - 1; y <= j+1; y++ {
142
+ if x >= 0 && x < m && y >= 0 && y < n && land[x][y] == 0 {
143
+ res += dfs(x, y)
144
+ }
248
145
}
249
146
}
147
+ return res
250
148
}
251
- s := make (map [int ]bool )
252
- var res []int
253
- for i := 0 ; i < m*n; i++ {
254
- if land[i/n][i%n] != 0 {
255
- continue
256
- }
257
- root := find (i)
258
- if !s[root] {
259
- s[root] = true
260
- res = append (res, size[root])
149
+ for i := range land {
150
+ for j := range land[i] {
151
+ if land[i][j] == 0 {
152
+ ans = append(ans, dfs(i, j))
153
+ }
261
154
}
262
155
}
263
- sort.Ints (res)
264
- return res
265
- }
266
-
267
- func find (x int ) int {
268
- if p[x] != x {
269
- p[x] = find (p[x])
270
- }
271
- return p[x]
156
+ sort.Ints(ans)
157
+ return
272
158
}
159
+ ```
273
160
274
- func union (a , b int ) {
275
- pa , pb := find (a), find (b)
276
- if pa == pb {
277
- return
278
- }
279
- size[pb] += size[pa]
280
- p[pa] = pb
161
+ ### ** TypeScript**
162
+
163
+ ``` ts
164
+ function pondSizes(land : number [][]): number [] {
165
+ const m = land .length ;
166
+ const n = land [0 ].length ;
167
+ const dfs = (i : number , j : number ): number => {
168
+ let res = 1 ;
169
+ land [i ][j ] = 1 ;
170
+ for (let x = i - 1 ; x <= i + 1 ; ++ x ) {
171
+ for (let y = j - 1 ; y <= j + 1 ; ++ y ) {
172
+ if (x >= 0 && x < m && y >= 0 && y < n && land [x ][y ] === 0 ) {
173
+ res += dfs (x , y );
174
+ }
175
+ }
176
+ }
177
+ return res ;
178
+ };
179
+ const ans: number [] = [];
180
+ for (let i = 0 ; i < m ; ++ i ) {
181
+ for (let j = 0 ; j < n ; ++ j ) {
182
+ if (land [i ][j ] === 0 ) {
183
+ ans .push (dfs (i , j ));
184
+ }
185
+ }
186
+ }
187
+ ans .sort ((a , b ) => a - b );
188
+ return ans ;
281
189
}
282
190
```
283
191
0 commit comments