forked from shuboc/LeetCode-2
-
Notifications
You must be signed in to change notification settings - Fork 1
/
number-of-islands-ii.cpp
119 lines (106 loc) · 4.16 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
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
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
// Time: O(klog*k) ~= O(k), k is the length of the positions
// Space: O(k)
// Using unordered_map.
class Solution {
public:
vector<int> numIslands2(int m, int n, vector<pair<int, int>>& positions) {
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& position : positions) {
const auto& node = make_pair(position.first, position.second);
set[node_id(node, n)] = node_id(node, n);
++number;
for (const auto& d : directions) {
const auto& neighbor = make_pair(position.first + d.first,
position.second + d.second);
if (neighbor.first >= 0 && neighbor.first < m &&
neighbor.second >= 0 && neighbor.second < n &&
set.find(node_id(neighbor, n)) != set.end()) {
if (find_set(node_id(node, n), &set) !=
find_set(node_id(neighbor, n), &set)) {
// Merge different islands, amortised time: O(log*k) ~= O(1)
union_set(&set, node_id(node, n), node_id(neighbor, n));
--number;
}
}
}
numbers.emplace_back(number);
}
return numbers;
}
int node_id(const pair<int, int>& node, const int n) {
return node.first * n + 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);
}
};
// Time: O(klog*k) ~= O(k), k is the length of the positions
// Space: O(m * n)
// Using vector.
class Solution2 {
public:
/**
* @param n an integer
* @param m an integer
* @param operators an array of point
* @return an integer array
*/
vector<int> numIslands2(int m, int n, vector<pair<int, int>>& positions) {
vector<int> numbers;
int number = 0;
const vector<pair<int, int>> directions{{0, -1}, {0, 1},
{-1, 0}, {1, 0}};
vector<int> set(m * n, -1);
for (const auto& position : positions) {
const auto& node = make_pair(position.first, position.second);
set[node_id(node, n)] = node_id(node, n);
++number;
for (const auto& d : directions) {
const auto& neighbor = make_pair(position.first + d.first,
position.second + d.second);
if (neighbor.first >= 0 && neighbor.first < m &&
neighbor.second >= 0 && neighbor.second < n &&
set[node_id(neighbor, n)] != -1) {
if (find_set(node_id(node, n), &set) !=
find_set(node_id(neighbor, n), &set)) {
// Merge different islands, amortised time: O(log*k) ~= O(1)
union_set(&set, node_id(node, n), node_id(neighbor, n));
--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, vector<int> *set) {
int parent = x;
while ((*set)[parent] != parent) {
parent = (*set)[parent];
}
while ((*set)[x] != x) {
int tmp = (*set)[x];
(*set)[x] = parent;
x = tmp;
}
return parent;
}
void union_set(vector<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);
}
};