-
Notifications
You must be signed in to change notification settings - Fork 257
/
number-of-islands-ii.cpp
68 lines (61 loc) · 2.15 KB
/
number-of-islands-ii.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
67
68
// Time: O(k), k is the length of the operators.
// Space: O(k)
/**
* Definition for a point.
* struct Point {
* int x;
* int y;
* Point() : x(0), y(0) {}
* Point(int a, int b) : x(a), y(b) {}
* };
*/
class Solution {
public:
/**
* @param n an integer
* @param m an integer
* @param operators an array of point
* @return an integer array
*/
vector<int> numIslands2(int n, int m, vector<Point>& operators) {
vector<int> numbers;
int number = 0;
const vector<pair<int, int>> directions{{0, -1}, {0, 1},
{-1, 0}, {1, 0}};
unordered_map<int, int> set;
for (const auto& oper : operators) {
const auto& node = make_pair(oper.x, oper.y);
set[node_id(node, m)] = node_id(node, m);
++number;
for (const auto& d : directions) {
const auto& neighbor = make_pair(oper.x + d.first,
oper.y + d.second);
if (neighbor.first >= 0 && neighbor.first < n &&
neighbor.second >= 0 && neighbor.second < m &&
set.find(node_id(neighbor, m)) != set.end()) {
if (find_set(node_id(node, m), &set) !=
find_set(node_id(neighbor, m), &set)) {
// Merge different islands.
union_set(&set, node_id(node, m), node_id(neighbor, m));
--number;
}
}
}
numbers.emplace_back(number);
}
return numbers;
}
int node_id(const pair<int, int>& node, const int m) {
return node.first * m + node.second;
}
int find_set(int x, unordered_map<int, int> *set) {
if ((*set)[x] != x) {
(*set)[x] = find_set((*set)[x], set); // path compression.
}
return (*set)[x];
}
void union_set(unordered_map<int, int> *set, const int x, const int y) {
int x_root = find_set(x, set), y_root = find_set(y, set);
(*set)[min(x_root, y_root)] = max(x_root, y_root);
}
};