-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathrankTransformofaMatrix.cpp
More file actions
58 lines (52 loc) · 1.38 KB
/
rankTransformofaMatrix.cpp
File metadata and controls
58 lines (52 loc) · 1.38 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
52
53
54
55
56
57
58
// Source: https://leetcode.com/problems/rank-transform-of-a-matrix/
// Author: Miao Zhang
// Date: 2021-05-24
class UnionFind {
public:
UnionFind(int n): p_(n) {
iota(begin(p_), end(p_), 0);
}
int find(int x) {
if (x != p_[x]) {
p_[x] = find(p_[x]);
}
return p_[x];
}
void merge(int x, int y) {
int px = find(x);
int py = find(y);
if (px != py) {
p_[px] = py;
}
}
private:
vector<int> p_;
};
class Solution {
public:
vector<vector<int>> matrixRankTransform(vector<vector<int>>& matrix) {
int m = matrix.size();
int n = matrix[0].size();
vector<int> rank(m + n);
map<int, vector<pair<int, int>>> dic;
for (int i = 0; i < m; i++) {
for (int j = 0; j < n; j++) {
dic[matrix[i][j]].emplace_back(i, j);
}
}
for (auto& [k, v]: dic) {
UnionFind uf(m + n);
vector<int> rank2(rank);
for (auto& [i, j]: v) {
int pi = uf.find(i);
int pj = uf.find(j + m);
uf.merge(pi, pj);
rank2[pj] = max(rank2[pi], rank2[pj]);
}
for (auto& [i, j]: v) {
rank[i] = rank[j + m] = matrix[i][j] = rank2[uf.find(i)] + 1;
}
}
return matrix;
}
};