forked from upupming/algorithm
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy path1632.cpp
66 lines (59 loc) · 2.49 KB
/
1632.cpp
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
59
60
61
62
63
64
65
66
// https://leetcode.com/contest/weekly-contest-212/problems/path-with-minimum-effort/
#include <bits/stdc++.h>
using namespace std;
class Solution {
public:
vector<vector<int>> matrixRankTransform(vector<vector<int>>& matrix) {
int m = matrix.size(), n = matrix[0].size();
// (m + n) vertices, [0, m) represents rows [0, m) and [m, m + n) represents cols [0, n)
vector<int> rank(m + n);
vector<vector<int>> ans(m, vector<int>(n));
// Generate edges with same matrix value
map<int, vector<int>> mp;
for (int i = 0; i < m; i++) {
for (int j = 0; j < n; j++) {
mp[matrix[i][j]].push_back(i * n + j);
}
}
// For edges with smaller value to larger value
for (auto& a : mp) {
// Root array
vector<int> root(m + n);
// init root[i] = i
iota(root.begin(), root.end(), 0);
auto values = a.second;
for (auto& v : values) {
int i = v / n, j = v % n;
cout << "(i, j) = (" << i << ", " << j << ")" << endl;
// find the root for vertex i and j + m
int r1 = find(root, i), r2 = find(root, j + m);
// make row vertex point to column vertex
root[r1] = r2;
cout << "set root of " << r1 << " to " << r2 << endl;
// update the root's rank
rank[r2] = max(rank[r1], rank[r2]);
cout << "update rank of " << r2 << " to " << rank[r2] << endl;
}
// make a deep copy for the convenience of calculation
auto rank2 = rank;
for (auto& v : values) {
int i = v / n, j = v % n;
cout << "(i, j) = (" << i << ", " << j << ")" << endl;
int r = find(root, i);
cout << "found root of " << i << " is " << r << endl;
ans[i][j] = rank[r] + 1;
cout << "update ans[" << i << "][" << j << "] to " << ans[i][j] << endl;
rank2[i] = rank[r] + 1;
cout << "update rank2[" << i << "] to " << ans[i][j] << endl;
rank2[j + m] = rank[r] + 1;
cout << "update rank2[" << j + m << "] to " << rank2[j + m] << endl;
}
rank = move(rank2);
}
return ans;
}
int find(const vector<int>& root, int i) {
if (root[i] != i) return find(root, root[i]);
return root[i];
}
};