Skip to content

Commit 16a4493

Browse files
authored
Create rank-transform-of-a-matrix.cpp
1 parent dbe2599 commit 16a4493

File tree

1 file changed

+78
-0
lines changed

1 file changed

+78
-0
lines changed

C++/rank-transform-of-a-matrix.cpp

Lines changed: 78 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,78 @@
1+
// Time: O(m * n * log(m * n) + m * n * α(m * n)) = O(m * n * log(m * n))
2+
// Space: O(m * n)
3+
4+
class Solution {
5+
public:
6+
vector<vector<int>> matrixRankTransform(vector<vector<int>>& matrix) {
7+
map<int, vector<pair<int, int>>> lookup;
8+
for (int i = 0; i < size(matrix); ++i) {
9+
for (int j = 0; j < size(matrix[0]); ++j) {
10+
lookup[matrix[i][j]].emplace_back(i, j);
11+
}
12+
}
13+
vector<int> rank(size(matrix) + size(matrix[0]));
14+
for (const auto& [x, pairs] : lookup) {
15+
vector<int> new_rank(rank);
16+
const auto& cb = [&new_rank](int x, int y, int z) {
17+
new_rank[x] = max(new_rank[y], new_rank[z]);
18+
};
19+
UnionFind union_find(size(matrix) + size(matrix[0]), cb);
20+
for (const auto& [i, j] : pairs) {
21+
union_find.union_set(i, j + size(matrix));
22+
}
23+
for (const auto& [i, j] : pairs) {
24+
matrix[i][j] = rank[i] = rank[j + size(matrix)] = new_rank[union_find.find_set(i)] + 1;
25+
}
26+
}
27+
return matrix;
28+
}
29+
30+
private:
31+
class UnionFind {
32+
public:
33+
UnionFind(const int n, function<void(int, int, int)> cb)
34+
: set_(n)
35+
, rank_(n)
36+
, count_(n)
37+
, cb_(cb) {
38+
iota(set_.begin(), set_.end(), 0);
39+
}
40+
41+
int find_set(const int x) {
42+
if (set_[x] != x) {
43+
set_[x] = find_set(set_[x]); // Path compression.
44+
}
45+
return set_[x];
46+
}
47+
48+
bool union_set(const int x, const int y) {
49+
int x_root = find_set(x), y_root = find_set(y);
50+
if (x_root == y_root) {
51+
return false;
52+
}
53+
if (rank_[x_root] < rank_[y_root]) { // Union by rank.
54+
set_[x_root] = y_root;
55+
cb_(y_root, x_root, y_root);
56+
} else if (rank_[x_root] > rank_[y_root]) {
57+
set_[y_root] = x_root;
58+
cb_(x_root, x_root, y_root);
59+
} else {
60+
set_[y_root] = x_root;
61+
++rank_[x_root];
62+
cb_(x_root, x_root, y_root);
63+
}
64+
--count_;
65+
return true;
66+
}
67+
68+
int size() const {
69+
return count_;
70+
}
71+
72+
private:
73+
vector<int> set_;
74+
vector<int> rank_;
75+
int count_;
76+
function<void(int, int, int)> cb_;
77+
};
78+
};

0 commit comments

Comments
 (0)