-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathProblem_0947_removeStones.cc
98 lines (86 loc) · 1.61 KB
/
Problem_0947_removeStones.cc
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
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
#include <numeric>
#include <unordered_map>
#include <vector>
#include "UnitTest.h"
using namespace std;
class Solution
{
private:
// key: 某行
// value: 第一次遇到石头的编号
unordered_map<int, int> rowFirst;
unordered_map<int, int> colFirst;
vector<int> father;
int number;
private:
void build(int n)
{
rowFirst.clear();
colFirst.clear();
father.resize(n);
std::iota(father.begin(), father.end(), 0);
number = n;
}
int find(int i)
{
if (i != father[i])
{
father[i] = find(father[i]);
}
return father[i];
}
void unions(int x, int y)
{
int fx = find(x);
int fy = find(y);
if (fx != fy)
{
father[fx] = fy;
number--;
}
}
public:
int removeStones(vector<vector<int>>& stones)
{
int n = stones.size();
build(n);
for (int i = 0; i < n; i++)
{
int row = stones[i][0];
int col = stones[i][1];
if (!rowFirst.count(row))
{
rowFirst[row] = i;
}
else
{
unions(i, rowFirst[row]);
}
if (!colFirst.count(col))
{
colFirst[col] = i;
}
else
{
unions(i, colFirst[col]);
}
}
return n - number;
}
};
void test()
{
Solution s;
vector<vector<int>> s1 = {{0, 0}, {0, 1}, {1, 0}, {1, 2}, {2, 1}, {2, 2}};
vector<vector<int>> s2 = {{0, 0}, {0, 2}, {1, 1}, {2, 0}, {2, 2}};
vector<vector<int>> s3 = {{0, 0}};
EXPECT_EQ_INT(5, s.removeStones(s1));
EXPECT_EQ_INT(3, s.removeStones(s2));
EXPECT_EQ_INT(0, s.removeStones(s3));
EXPECT_SUMMARY;
}
int main()
{
test();
return 0;
}