generated from threeal/project-starter
-
Notifications
You must be signed in to change notification settings - Fork 1
/
solution.cpp
57 lines (51 loc) · 1.58 KB
/
solution.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
#include <unordered_map>
#include <vector>
class Solution {
public:
int removeStones(std::vector<std::vector<int>>& stones) {
int removed{};
std::unordered_map<int, int> hParents{};
std::unordered_map<int, int> vParents{};
for (int i = stones.size() - 1; i >= 0; --i) {
const auto hIt = hParents.find(stones[i][0]);
const auto vIt = vParents.find(stones[i][1]);
if (hIt == hParents.end()) {
if (vIt == vParents.end()) {
hParents.emplace(stones[i][0], i);
vParents.emplace(stones[i][1], i);
} else {
hParents.emplace(stones[i][0], vIt->second);
++removed;
}
} else {
if (vIt == vParents.end()) {
vParents.emplace(stones[i][1], hIt->second);
++removed;
} else {
int hRoot = findHRoot(hParents, stones, hIt->second);
int vRoot = findHRoot(hParents, stones, vIt->second);
if (hRoot != vRoot) {
++removed;
if (hRoot > vRoot) {
hParents[stones[vRoot][0]] = hRoot;
vParents[stones[vRoot][1]] = hRoot;
} else {
hParents[stones[hRoot][0]] = vRoot;
vParents[stones[hRoot][1]] = vRoot;
}
}
++removed;
}
}
}
return removed;
}
private:
static int findHRoot(
std::unordered_map<int, int>& hParents,
const std::vector<std::vector<int>>& stones, int i) {
auto& parent = hParents[stones[i][0]];
if (parent != i) parent = findHRoot(hParents, stones, parent);
return parent;
}
};