Skip to content

Commit ecc9aa7

Browse files
committed
feat: update solutions to lc problem: No.1632
No.1632.Rank Transform of a Matrix
1 parent abdc7f5 commit ecc9aa7

File tree

6 files changed

+123
-21
lines changed

6 files changed

+123
-21
lines changed

solution/1600-1699/1632.Rank Transform of a Matrix/README.md

+41-7
Original file line numberDiff line numberDiff line change
@@ -111,6 +111,10 @@ class UnionFind:
111111
self.p[pa] = pb
112112
self.size[pb] += self.size[pa]
113113

114+
def reset(self, x):
115+
self.p[x] = x
116+
self.size[x] = 1
117+
114118

115119
class Solution:
116120
def matrixRankTransform(self, matrix: List[List[int]]) -> List[List[int]]:
@@ -122,15 +126,18 @@ class Solution:
122126
row_max = [0] * m
123127
col_max = [0] * n
124128
ans = [[0] * n for _ in range(m)]
129+
uf = UnionFind(m + n)
125130
for v in sorted(d):
126-
uf = UnionFind(m + n)
127131
rank = defaultdict(int)
128132
for i, j in d[v]:
129133
uf.union(i, j + m)
130134
for i, j in d[v]:
131135
rank[uf.find(i)] = max(rank[uf.find(i)], row_max[i], col_max[j])
132136
for i, j in d[v]:
133137
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)
134141
return ans
135142
```
136143

@@ -171,6 +178,11 @@ class UnionFind {
171178
}
172179
}
173180
}
181+
182+
public void reset(int x) {
183+
p[x] = x;
184+
size[x] = 1;
185+
}
174186
}
175187

176188

@@ -186,9 +198,9 @@ class Solution {
186198
int[] rowMax = new int[m];
187199
int[] colMax = new int[n];
188200
int[][] ans = new int[m][n];
201+
UnionFind uf = new UnionFind(m + n);
202+
int[] rank = new int[m + n];
189203
for (var ps : d.values()) {
190-
UnionFind uf = new UnionFind(m + n);
191-
int[] rank = new int[m + n];
192204
for (var p : ps) {
193205
uf.union(p[0], p[1] + m);
194206
}
@@ -202,6 +214,10 @@ class Solution {
202214
rowMax[i] = ans[i][j];
203215
colMax[j] = ans[i][j];
204216
}
217+
for (var p : ps) {
218+
uf.reset(p[0]);
219+
uf.reset(p[1] + m);
220+
}
205221
}
206222
return ans;
207223
}
@@ -239,6 +255,11 @@ public:
239255
return p[x];
240256
}
241257

258+
void reset(int x) {
259+
p[x] = x;
260+
size[x] = 1;
261+
}
262+
242263
private:
243264
vector<int> p, size;
244265
};
@@ -256,9 +277,9 @@ public:
256277
vector<int> rowMax(m);
257278
vector<int> colMax(n);
258279
vector<vector<int>> ans(m, vector<int>(n));
280+
UnionFind uf(m + n);
281+
vector<int> rank(m + n);
259282
for (auto& [_, ps] : d) {
260-
UnionFind uf(m + n);
261-
vector<int> rank(m + n);
262283
for (auto& [i, j] : ps) {
263284
uf.unite(i, j + m);
264285
}
@@ -268,6 +289,10 @@ public:
268289
for (auto& [i, j] : ps) {
269290
ans[i][j] = rowMax[i] = colMax[j] = 1 + rank[uf.find(i)];
270291
}
292+
for (auto& [i, j] : ps) {
293+
uf.reset(i);
294+
uf.reset(j + m);
295+
}
271296
}
272297
return ans;
273298
}
@@ -311,6 +336,11 @@ func (uf *unionFind) union(a, b int) {
311336
}
312337
}
313338
339+
func (uf *unionFind) reset(x int) {
340+
uf.p[x] = x
341+
uf.size[x] = 1
342+
}
343+
314344
func matrixRankTransform(matrix [][]int) [][]int {
315345
m, n := len(matrix), len(matrix[0])
316346
type pair struct{ i, j int }
@@ -331,10 +361,10 @@ func matrixRankTransform(matrix [][]int) [][]int {
331361
vs = append(vs, v)
332362
}
333363
sort.Ints(vs)
364+
uf := newUnionFind(m + n)
365+
rank := make([]int, m+n)
334366
for _, v := range vs {
335367
ps := d[v]
336-
uf := newUnionFind(m + n)
337-
rank := make([]int, m+n)
338368
for _, p := range ps {
339369
uf.union(p.i, p.j+m)
340370
}
@@ -347,6 +377,10 @@ func matrixRankTransform(matrix [][]int) [][]int {
347377
ans[i][j] = 1 + rank[uf.find(i)]
348378
rowMax[i], colMax[j] = ans[i][j], ans[i][j]
349379
}
380+
for _, p := range ps {
381+
uf.reset(p.i)
382+
uf.reset(p.j + m)
383+
}
350384
}
351385
return ans
352386
}

solution/1600-1699/1632.Rank Transform of a Matrix/README_EN.md

+41-7
Original file line numberDiff line numberDiff line change
@@ -86,6 +86,10 @@ class UnionFind:
8686
self.p[pa] = pb
8787
self.size[pb] += self.size[pa]
8888

89+
def reset(self, x):
90+
self.p[x] = x
91+
self.size[x] = 1
92+
8993

9094
class Solution:
9195
def matrixRankTransform(self, matrix: List[List[int]]) -> List[List[int]]:
@@ -97,15 +101,18 @@ class Solution:
97101
row_max = [0] * m
98102
col_max = [0] * n
99103
ans = [[0] * n for _ in range(m)]
104+
uf = UnionFind(m + n)
100105
for v in sorted(d):
101-
uf = UnionFind(m + n)
102106
rank = defaultdict(int)
103107
for i, j in d[v]:
104108
uf.union(i, j + m)
105109
for i, j in d[v]:
106110
rank[uf.find(i)] = max(rank[uf.find(i)], row_max[i], col_max[j])
107111
for i, j in d[v]:
108112
ans[i][j] = row_max[i] = col_max[j] = 1 + rank[uf.find(i)]
113+
for i, j in d[v]:
114+
uf.reset(i)
115+
uf.reset(j + m)
109116
return ans
110117
```
111118

@@ -144,6 +151,11 @@ class UnionFind {
144151
}
145152
}
146153
}
154+
155+
public void reset(int x) {
156+
p[x] = x;
157+
size[x] = 1;
158+
}
147159
}
148160

149161

@@ -159,9 +171,9 @@ class Solution {
159171
int[] rowMax = new int[m];
160172
int[] colMax = new int[n];
161173
int[][] ans = new int[m][n];
174+
UnionFind uf = new UnionFind(m + n);
175+
int[] rank = new int[m + n];
162176
for (var ps : d.values()) {
163-
UnionFind uf = new UnionFind(m + n);
164-
int[] rank = new int[m + n];
165177
for (var p : ps) {
166178
uf.union(p[0], p[1] + m);
167179
}
@@ -175,6 +187,10 @@ class Solution {
175187
rowMax[i] = ans[i][j];
176188
colMax[j] = ans[i][j];
177189
}
190+
for (var p : ps) {
191+
uf.reset(p[0]);
192+
uf.reset(p[1] + m);
193+
}
178194
}
179195
return ans;
180196
}
@@ -212,6 +228,11 @@ public:
212228
return p[x];
213229
}
214230

231+
void reset(int x) {
232+
p[x] = x;
233+
size[x] = 1;
234+
}
235+
215236
private:
216237
vector<int> p, size;
217238
};
@@ -229,9 +250,9 @@ public:
229250
vector<int> rowMax(m);
230251
vector<int> colMax(n);
231252
vector<vector<int>> ans(m, vector<int>(n));
253+
UnionFind uf(m + n);
254+
vector<int> rank(m + n);
232255
for (auto& [_, ps] : d) {
233-
UnionFind uf(m + n);
234-
vector<int> rank(m + n);
235256
for (auto& [i, j] : ps) {
236257
uf.unite(i, j + m);
237258
}
@@ -241,6 +262,10 @@ public:
241262
for (auto& [i, j] : ps) {
242263
ans[i][j] = rowMax[i] = colMax[j] = 1 + rank[uf.find(i)];
243264
}
265+
for (auto& [i, j] : ps) {
266+
uf.reset(i);
267+
uf.reset(j + m);
268+
}
244269
}
245270
return ans;
246271
}
@@ -284,6 +309,11 @@ func (uf *unionFind) union(a, b int) {
284309
}
285310
}
286311
312+
func (uf *unionFind) reset(x int) {
313+
uf.p[x] = x
314+
uf.size[x] = 1
315+
}
316+
287317
func matrixRankTransform(matrix [][]int) [][]int {
288318
m, n := len(matrix), len(matrix[0])
289319
type pair struct{ i, j int }
@@ -304,10 +334,10 @@ func matrixRankTransform(matrix [][]int) [][]int {
304334
vs = append(vs, v)
305335
}
306336
sort.Ints(vs)
337+
uf := newUnionFind(m + n)
338+
rank := make([]int, m+n)
307339
for _, v := range vs {
308340
ps := d[v]
309-
uf := newUnionFind(m + n)
310-
rank := make([]int, m+n)
311341
for _, p := range ps {
312342
uf.union(p.i, p.j+m)
313343
}
@@ -320,6 +350,10 @@ func matrixRankTransform(matrix [][]int) [][]int {
320350
ans[i][j] = 1 + rank[uf.find(i)]
321351
rowMax[i], colMax[j] = ans[i][j], ans[i][j]
322352
}
353+
for _, p := range ps {
354+
uf.reset(p.i)
355+
uf.reset(p.j + m)
356+
}
323357
}
324358
return ans
325359
}

solution/1600-1699/1632.Rank Transform of a Matrix/Solution.cpp

+11-2
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,11 @@ class UnionFind {
2626
return p[x];
2727
}
2828

29+
void reset(int x) {
30+
p[x] = x;
31+
size[x] = 1;
32+
}
33+
2934
private:
3035
vector<int> p, size;
3136
};
@@ -43,9 +48,9 @@ class Solution {
4348
vector<int> rowMax(m);
4449
vector<int> colMax(n);
4550
vector<vector<int>> ans(m, vector<int>(n));
51+
UnionFind uf(m + n);
52+
vector<int> rank(m + n);
4653
for (auto& [_, ps] : d) {
47-
UnionFind uf(m + n);
48-
vector<int> rank(m + n);
4954
for (auto& [i, j] : ps) {
5055
uf.unite(i, j + m);
5156
}
@@ -55,6 +60,10 @@ class Solution {
5560
for (auto& [i, j] : ps) {
5661
ans[i][j] = rowMax[i] = colMax[j] = 1 + rank[uf.find(i)];
5762
}
63+
for (auto& [i, j] : ps) {
64+
uf.reset(i);
65+
uf.reset(j + m);
66+
}
5867
}
5968
return ans;
6069
}

solution/1600-1699/1632.Rank Transform of a Matrix/Solution.go

+11-2
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,11 @@ func (uf *unionFind) union(a, b int) {
3232
}
3333
}
3434

35+
func (uf *unionFind) reset(x int) {
36+
uf.p[x] = x
37+
uf.size[x] = 1
38+
}
39+
3540
func matrixRankTransform(matrix [][]int) [][]int {
3641
m, n := len(matrix), len(matrix[0])
3742
type pair struct{ i, j int }
@@ -52,10 +57,10 @@ func matrixRankTransform(matrix [][]int) [][]int {
5257
vs = append(vs, v)
5358
}
5459
sort.Ints(vs)
60+
uf := newUnionFind(m + n)
61+
rank := make([]int, m+n)
5562
for _, v := range vs {
5663
ps := d[v]
57-
uf := newUnionFind(m + n)
58-
rank := make([]int, m+n)
5964
for _, p := range ps {
6065
uf.union(p.i, p.j+m)
6166
}
@@ -68,6 +73,10 @@ func matrixRankTransform(matrix [][]int) [][]int {
6873
ans[i][j] = 1 + rank[uf.find(i)]
6974
rowMax[i], colMax[j] = ans[i][j], ans[i][j]
7075
}
76+
for _, p := range ps {
77+
uf.reset(p.i)
78+
uf.reset(p.j + m)
79+
}
7180
}
7281
return ans
7382
}

solution/1600-1699/1632.Rank Transform of a Matrix/Solution.java

+11-2
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,11 @@ public void union(int a, int b) {
3030
}
3131
}
3232
}
33+
34+
public void reset(int x) {
35+
p[x] = x;
36+
size[x] = 1;
37+
}
3338
}
3439

3540

@@ -45,9 +50,9 @@ public int[][] matrixRankTransform(int[][] matrix) {
4550
int[] rowMax = new int[m];
4651
int[] colMax = new int[n];
4752
int[][] ans = new int[m][n];
53+
UnionFind uf = new UnionFind(m + n);
54+
int[] rank = new int[m + n];
4855
for (var ps : d.values()) {
49-
UnionFind uf = new UnionFind(m + n);
50-
int[] rank = new int[m + n];
5156
for (var p : ps) {
5257
uf.union(p[0], p[1] + m);
5358
}
@@ -61,6 +66,10 @@ public int[][] matrixRankTransform(int[][] matrix) {
6166
rowMax[i] = ans[i][j];
6267
colMax[j] = ans[i][j];
6368
}
69+
for (var p : ps) {
70+
uf.reset(p[0]);
71+
uf.reset(p[1] + m);
72+
}
6473
}
6574
return ans;
6675
}

0 commit comments

Comments
 (0)