@@ -111,6 +111,10 @@ class UnionFind:
111
111
self .p[pa] = pb
112
112
self .size[pb] += self .size[pa]
113
113
114
+ def reset (self , x ):
115
+ self .p[x] = x
116
+ self .size[x] = 1
117
+
114
118
115
119
class Solution :
116
120
def matrixRankTransform (self , matrix : List[List[int ]]) -> List[List[int ]]:
@@ -122,15 +126,18 @@ class Solution:
122
126
row_max = [0 ] * m
123
127
col_max = [0 ] * n
124
128
ans = [[0 ] * n for _ in range (m)]
129
+ uf = UnionFind(m + n)
125
130
for v in sorted (d):
126
- uf = UnionFind(m + n)
127
131
rank = defaultdict(int )
128
132
for i, j in d[v]:
129
133
uf.union(i, j + m)
130
134
for i, j in d[v]:
131
135
rank[uf.find(i)] = max (rank[uf.find(i)], row_max[i], col_max[j])
132
136
for i, j in d[v]:
133
137
ans[i][j] = row_max[i] = col_max[j] = 1 + rank[uf.find(i)]
138
+ for i, j in d[v]:
139
+ uf.reset(i)
140
+ uf.reset(j + m)
134
141
return ans
135
142
```
136
143
@@ -171,6 +178,11 @@ class UnionFind {
171
178
}
172
179
}
173
180
}
181
+
182
+ public void reset (int x ) {
183
+ p[x] = x;
184
+ size[x] = 1 ;
185
+ }
174
186
}
175
187
176
188
@@ -186,9 +198,9 @@ class Solution {
186
198
int [] rowMax = new int [m];
187
199
int [] colMax = new int [n];
188
200
int [][] ans = new int [m][n];
201
+ UnionFind uf = new UnionFind (m + n);
202
+ int [] rank = new int [m + n];
189
203
for (var ps : d. values()) {
190
- UnionFind uf = new UnionFind (m + n);
191
- int [] rank = new int [m + n];
192
204
for (var p : ps) {
193
205
uf. union(p[0 ], p[1 ] + m);
194
206
}
@@ -202,6 +214,10 @@ class Solution {
202
214
rowMax[i] = ans[i][j];
203
215
colMax[j] = ans[i][j];
204
216
}
217
+ for (var p : ps) {
218
+ uf. reset(p[0 ]);
219
+ uf. reset(p[1 ] + m);
220
+ }
205
221
}
206
222
return ans;
207
223
}
@@ -239,6 +255,11 @@ public:
239
255
return p[ x] ;
240
256
}
241
257
258
+ void reset(int x) {
259
+ p[x] = x;
260
+ size[x] = 1;
261
+ }
262
+
242
263
private:
243
264
vector<int > p, size;
244
265
};
@@ -256,9 +277,9 @@ public:
256
277
vector<int > rowMax(m);
257
278
vector<int > colMax(n);
258
279
vector<vector<int >> ans(m, vector<int >(n));
280
+ UnionFind uf(m + n);
281
+ vector<int > rank(m + n);
259
282
for (auto& [ _ , ps] : d) {
260
- UnionFind uf(m + n);
261
- vector<int > rank(m + n);
262
283
for (auto& [ i, j] : ps) {
263
284
uf.unite(i, j + m);
264
285
}
@@ -268,6 +289,10 @@ public:
268
289
for (auto& [ i, j] : ps) {
269
290
ans[ i] [ j ] = rowMax[ i] = colMax[ j] = 1 + rank[ uf.find(i)] ;
270
291
}
292
+ for (auto& [ i, j] : ps) {
293
+ uf.reset(i);
294
+ uf.reset(j + m);
295
+ }
271
296
}
272
297
return ans;
273
298
}
@@ -311,6 +336,11 @@ func (uf *unionFind) union(a, b int) {
311
336
}
312
337
}
313
338
339
+ func (uf *unionFind) reset(x int) {
340
+ uf.p[x] = x
341
+ uf.size[x] = 1
342
+ }
343
+
314
344
func matrixRankTransform(matrix [][]int) [][]int {
315
345
m, n := len(matrix), len(matrix[0])
316
346
type pair struct{ i, j int }
@@ -331,10 +361,10 @@ func matrixRankTransform(matrix [][]int) [][]int {
331
361
vs = append(vs, v)
332
362
}
333
363
sort.Ints(vs)
364
+ uf := newUnionFind(m + n)
365
+ rank := make([]int, m+n)
334
366
for _, v := range vs {
335
367
ps := d[v]
336
- uf := newUnionFind(m + n)
337
- rank := make([]int, m+n)
338
368
for _, p := range ps {
339
369
uf.union(p.i, p.j+m)
340
370
}
@@ -347,6 +377,10 @@ func matrixRankTransform(matrix [][]int) [][]int {
347
377
ans[i][j] = 1 + rank[uf.find(i)]
348
378
rowMax[i], colMax[j] = ans[i][j], ans[i][j]
349
379
}
380
+ for _, p := range ps {
381
+ uf.reset(p.i)
382
+ uf.reset(p.j + m)
383
+ }
350
384
}
351
385
return ans
352
386
}
0 commit comments