-
Notifications
You must be signed in to change notification settings - Fork 5
/
Copy pathmost_stones_removed_with_same_row_or_column.dart
202 lines (166 loc) · 5.39 KB
/
most_stones_removed_with_same_row_or_column.dart
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
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
/*
-* Most Stones Removed with Same Row or Column *-
On a 2D plane, we place n stones at some integer coordinate points. Each coordinate point may have at most one stone.
A stone can be removed if it shares either the same row or the same column as another stone that has not been removed.
Given an array stones of length n where stones[i] = [xi, yi] represents the location of the ith stone, return the largest possible number of stones that can be removed.
Example 1:
Input: stones = [[0,0],[0,1],[1,0],[1,2],[2,1],[2,2]]
Output: 5
Explanation: One way to remove 5 stones is as follows:
1. Remove stone [2,2] because it shares the same row as [2,1].
2. Remove stone [2,1] because it shares the same column as [0,1].
3. Remove stone [1,2] because it shares the same row as [1,0].
4. Remove stone [1,0] because it shares the same column as [0,0].
5. Remove stone [0,1] because it shares the same row as [0,0].
Stone [0,0] cannot be removed since it does not share a row/column with another stone still on the plane.
Example 2:
Input: stones = [[0,0],[0,2],[1,1],[2,0],[2,2]]
Output: 3
Explanation: One way to make 3 moves is as follows:
1. Remove stone [2,2] because it shares the same row as [2,0].
2. Remove stone [2,0] because it shares the same column as [0,0].
3. Remove stone [0,2] because it shares the same row as [0,0].
Stones [0,0] and [1,1] cannot be removed since they do not share a row/column with another stone still on the plane.
Example 3:
Input: stones = [[0,0]]
Output: 0
Explanation: [0,0] is the only stone on the plane, so you cannot remove it.
Constraints:
1 <= stones.length <= 1000
0 <= xi, yi <= 104
No two stones are at the same coordinate point.
*/
import 'dart:collection';
// Union Find
// Runtime: 568 ms, faster than 100.00% of Dart online submissions for Most Stones Removed with Same Row or Column.
// Memory Usage: 151.2 MB, less than 100.00% of Dart online submissions for Most Stones Removed with Same Row or Column.
class A {
//Analysis
// Time Complexity: $O(nlogn)$.
// Space Complexity: $O(n)$.
int removeStones(List<List<int>> stones) {
if (stones.isEmpty || stones.length <= 1) {
return 0;
}
int n = stones.length;
UnionFind uf = UnionFind();
for (List<int> edge in stones) {
uf.union(edge[0] + 10001, edge[1]);
}
return n - uf.getCount();
}
}
class UnionFind {
late HashMap<int, int> parents;
late int count;
UnionFind() {
parents = HashMap();
count = 0;
}
int getCount() {
return count;
}
int find(int x) {
if (!parents.containsKey(x)) {
parents[x] = x;
count++;
}
if (x != parents[x]) {
parents[x] = find(parents[x]!);
}
return parents[x]!;
}
void union(int x, int y) {
int rootX = find(x);
int rootY = find(y);
if (rootX == rootY) {
return;
}
parents[rootX] = rootY;
count--;
}
}
// DFS
class B {
int removeStones(List<List<int>> stones) {
int n = stones.length;
List<bool> visited = List.filled(n, false);
int componentCount = 0;
for (int i = 0; i < n; i++) {
if (visited[i]) continue;
dfs(stones, visited, i);
componentCount++;
}
return (n - componentCount);
}
bool isValid(List<int> pos1, List<int> pos2) {
if (pos1[0] == pos2[0]) return true;
if (pos1[1] == pos2[1]) return true;
return false;
}
void dfs(List<List<int>> stones, List<bool> visited, int currentIdx) {
visited[currentIdx] = true;
for (int i = 0; i < stones.length; i++) {
if (visited[i]) continue;
if (isValid(stones[i], stones[currentIdx])) dfs(stones, visited, i);
}
}
}
class C {
int removeStones(List<List<int>> stones) {
int n = stones.length;
List<int> parent = [n];
for (int i = 0; i < n; i++) parent[i] = i;
//==============================================
int componentCount = n; //assuming "n" independent components at start
for (int i = 0; i < n; i++) {
for (int j = i; j < n; j++) {
if (isValid(stones[i], stones[j])) merge(parent, componentCount, i, j);
}
}
//===============================================
return (n - componentCount);
}
bool isValid(List<int> pos1, List<int> pos2) {
if (pos1[0] == pos2[0]) return true;
if (pos1[1] == pos2[1]) return true;
return false;
}
int findParent(List<int> parent, int node) {
while (parent[node] != node) {
node = parent[node];
}
return node;
}
void merge(List<int> parent, int componentCount, int node1, int node2) {
int parent1 = findParent(parent, node1);
int parent2 = findParent(parent, node2);
if (parent1 != parent2) {
componentCount--; //whenever we merge two components, totalComponentCount decrease by 1
parent[parent1] =
parent2; //or parent[parent2] = parent1 would also work :)
}
}
}
class D {
int removeStones(List<List<int>> stones) {
HashSet<List<int>> visited = HashSet();
int numOfIslands = 0;
for (List<int> s1 in stones) {
if (!visited.contains(s1)) {
dfs(s1, visited, stones);
numOfIslands++;
}
}
return stones.length - numOfIslands;
}
void dfs(List<int> s1, Set<List<int>> visited, List<List<int>> stones) {
visited.add(s1);
for (List<int> s2 in stones) {
if (!visited.contains(s2)) {
// stone with same row or column. group them into island
if (s1[0] == s2[0] || s1[1] == s2[1]) dfs(s2, visited, stones);
}
}
}
}