Skip to content

Commit 9951bdf

Browse files
authored
feat: add solutions to lc problem: No.0947 (doocs#3295)
1 parent c9e9bc6 commit 9951bdf

File tree

12 files changed

+1359
-157
lines changed

12 files changed

+1359
-157
lines changed

solution/0900-0999/0947.Most Stones Removed with Same Row or Column/README.md

+470-52
Large diffs are not rendered by default.

solution/0900-0999/0947.Most Stones Removed with Same Row or Column/README_EN.md

+470-52
Large diffs are not rendered by default.
Original file line numberDiff line numberDiff line change
@@ -1,19 +1,50 @@
1-
class Solution {
1+
class UnionFind {
22
public:
3-
vector<int> p;
3+
UnionFind(int n) {
4+
p = vector<int>(n);
5+
size = vector<int>(n, 1);
6+
iota(p.begin(), p.end(), 0);
7+
}
48

5-
int removeStones(vector<vector<int>>& stones) {
6-
int n = 10010;
7-
p.resize(n << 1);
8-
for (int i = 0; i < p.size(); ++i) p[i] = i;
9-
for (auto& stone : stones) p[find(stone[0])] = find(stone[1] + n);
10-
unordered_set<int> s;
11-
for (auto& stone : stones) s.insert(find(stone[0]));
12-
return stones.size() - s.size();
9+
bool unite(int a, int b) {
10+
int pa = find(a), pb = find(b);
11+
if (pa == pb) {
12+
return false;
13+
}
14+
if (size[pa] > size[pb]) {
15+
p[pb] = pa;
16+
size[pa] += size[pb];
17+
} else {
18+
p[pa] = pb;
19+
size[pb] += size[pa];
20+
}
21+
return true;
1322
}
1423

1524
int find(int x) {
16-
if (p[x] != x) p[x] = find(p[x]);
25+
if (p[x] != x) {
26+
p[x] = find(p[x]);
27+
}
1728
return p[x];
1829
}
30+
31+
private:
32+
vector<int> p, size;
33+
};
34+
35+
class Solution {
36+
public:
37+
int removeStones(vector<vector<int>>& stones) {
38+
int n = stones.size();
39+
UnionFind uf(n);
40+
int ans = 0;
41+
for (int i = 0; i < n; ++i) {
42+
for (int j = 0; j < i; ++j) {
43+
if (stones[i][0] == stones[j][0] || stones[i][1] == stones[j][1]) {
44+
ans += uf.unite(i, j);
45+
}
46+
}
47+
}
48+
return ans;
49+
}
1950
};
Original file line numberDiff line numberDiff line change
@@ -1,22 +1,50 @@
1-
func removeStones(stones [][]int) int {
2-
n := 10010
3-
p := make([]int, n<<1)
1+
type unionFind struct {
2+
p, size []int
3+
}
4+
5+
func newUnionFind(n int) *unionFind {
6+
p := make([]int, n)
7+
size := make([]int, n)
48
for i := range p {
59
p[i] = i
10+
size[i] = 1
611
}
7-
var find func(x int) int
8-
find = func(x int) int {
9-
if p[x] != x {
10-
p[x] = find(p[x])
11-
}
12-
return p[x]
12+
return &unionFind{p, size}
13+
}
14+
15+
func (uf *unionFind) find(x int) int {
16+
if uf.p[x] != x {
17+
uf.p[x] = uf.find(uf.p[x])
18+
}
19+
return uf.p[x]
20+
}
21+
22+
func (uf *unionFind) union(a, b int) bool {
23+
pa, pb := uf.find(a), uf.find(b)
24+
if pa == pb {
25+
return false
1326
}
14-
for _, stone := range stones {
15-
p[find(stone[0])] = find(stone[1] + n)
27+
if uf.size[pa] > uf.size[pb] {
28+
uf.p[pb] = pa
29+
uf.size[pa] += uf.size[pb]
30+
} else {
31+
uf.p[pa] = pb
32+
uf.size[pb] += uf.size[pa]
1633
}
17-
s := make(map[int]bool)
18-
for _, stone := range stones {
19-
s[find(stone[0])] = true
34+
return true
35+
}
36+
37+
func removeStones(stones [][]int) (ans int) {
38+
n := len(stones)
39+
uf := newUnionFind(n)
40+
for i, s1 := range stones {
41+
for j, s2 := range stones[:i] {
42+
if s1[0] == s2[0] || s1[1] == s2[1] {
43+
if uf.union(i, j) {
44+
ans++
45+
}
46+
}
47+
}
2048
}
21-
return len(stones) - len(s)
49+
return
2250
}
Original file line numberDiff line numberDiff line change
@@ -1,26 +1,51 @@
1-
class Solution {
2-
private int[] p;
1+
class UnionFind {
2+
private final int[] p;
3+
private final int[] size;
34

4-
public int removeStones(int[][] stones) {
5-
int n = 10010;
6-
p = new int[n << 1];
7-
for (int i = 0; i < p.length; ++i) {
5+
public UnionFind(int n) {
6+
p = new int[n];
7+
size = new int[n];
8+
for (int i = 0; i < n; ++i) {
89
p[i] = i;
10+
size[i] = 1;
911
}
10-
for (int[] stone : stones) {
11-
p[find(stone[0])] = find(stone[1] + n);
12-
}
13-
Set<Integer> s = new HashSet<>();
14-
for (int[] stone : stones) {
15-
s.add(find(stone[0]));
16-
}
17-
return stones.length - s.size();
1812
}
1913

20-
private int find(int x) {
14+
public int find(int x) {
2115
if (p[x] != x) {
2216
p[x] = find(p[x]);
2317
}
2418
return p[x];
2519
}
20+
21+
public boolean union(int a, int b) {
22+
int pa = find(a), pb = find(b);
23+
if (pa == pb) {
24+
return false;
25+
}
26+
if (size[pa] > size[pb]) {
27+
p[pb] = pa;
28+
size[pa] += size[pb];
29+
} else {
30+
p[pa] = pb;
31+
size[pb] += size[pa];
32+
}
33+
return true;
34+
}
35+
}
36+
37+
class Solution {
38+
public int removeStones(int[][] stones) {
39+
int n = stones.length;
40+
UnionFind uf = new UnionFind(n);
41+
int ans = 0;
42+
for (int i = 0; i < n; ++i) {
43+
for (int j = 0; j < i; ++j) {
44+
if (stones[i][0] == stones[j][0] || stones[i][1] == stones[j][1]) {
45+
ans += uf.union(i, j) ? 1 : 0;
46+
}
47+
}
48+
}
49+
return ans;
50+
}
2651
}
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,32 @@
1+
class UnionFind:
2+
def __init__(self, n):
3+
self.p = list(range(n))
4+
self.size = [1] * n
5+
6+
def find(self, x):
7+
if self.p[x] != x:
8+
self.p[x] = self.find(self.p[x])
9+
return self.p[x]
10+
11+
def union(self, a, b):
12+
pa, pb = self.find(a), self.find(b)
13+
if pa == pb:
14+
return False
15+
if self.size[pa] > self.size[pb]:
16+
self.p[pb] = pa
17+
self.size[pa] += self.size[pb]
18+
else:
19+
self.p[pa] = pb
20+
self.size[pb] += self.size[pa]
21+
return True
22+
23+
124
class Solution:
225
def removeStones(self, stones: List[List[int]]) -> int:
3-
def find(x):
4-
if p[x] != x:
5-
p[x] = find(p[x])
6-
return p[x]
7-
8-
n = 10010
9-
p = list(range(n << 1))
10-
for x, y in stones:
11-
p[find(x)] = find(y + n)
12-
13-
s = {find(x) for x, _ in stones}
14-
return len(stones) - len(s)
26+
uf = UnionFind(len(stones))
27+
ans = 0
28+
for i, (x1, y1) in enumerate(stones):
29+
for j, (x2, y2) in enumerate(stones[:i]):
30+
if x1 == x2 or y1 == y2:
31+
ans += uf.union(i, j)
32+
return ans
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,44 @@
1+
class UnionFind {
2+
p: number[];
3+
size: number[];
4+
constructor(n: number) {
5+
this.p = Array.from({ length: n }, (_, i) => i);
6+
this.size = Array(n).fill(1);
7+
}
8+
9+
find(x: number): number {
10+
if (this.p[x] !== x) {
11+
this.p[x] = this.find(this.p[x]);
12+
}
13+
return this.p[x];
14+
}
15+
16+
union(a: number, b: number): boolean {
17+
const [pa, pb] = [this.find(a), this.find(b)];
18+
if (pa === pb) {
19+
return false;
20+
}
21+
if (this.size[pa] > this.size[pb]) {
22+
this.p[pb] = pa;
23+
this.size[pa] += this.size[pb];
24+
} else {
25+
this.p[pa] = pb;
26+
this.size[pb] += this.size[pa];
27+
}
28+
return true;
29+
}
30+
}
31+
32+
function removeStones(stones: number[][]): number {
33+
const n = stones.length;
34+
const uf = new UnionFind(n);
35+
let ans = 0;
36+
for (let i = 0; i < n; ++i) {
37+
for (let j = 0; j < i; ++j) {
38+
if (stones[i][0] === stones[j][0] || stones[i][1] === stones[j][1]) {
39+
ans += uf.union(i, j) ? 1 : 0;
40+
}
41+
}
42+
}
43+
return ans;
44+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,49 @@
1+
class UnionFind {
2+
public:
3+
UnionFind(int n) {
4+
p = vector<int>(n);
5+
size = vector<int>(n, 1);
6+
iota(p.begin(), p.end(), 0);
7+
}
8+
9+
bool unite(int a, int b) {
10+
int pa = find(a), pb = find(b);
11+
if (pa == pb) {
12+
return false;
13+
}
14+
if (size[pa] > size[pb]) {
15+
p[pb] = pa;
16+
size[pa] += size[pb];
17+
} else {
18+
p[pa] = pb;
19+
size[pb] += size[pa];
20+
}
21+
return true;
22+
}
23+
24+
int find(int x) {
25+
if (p[x] != x) {
26+
p[x] = find(p[x]);
27+
}
28+
return p[x];
29+
}
30+
31+
private:
32+
vector<int> p, size;
33+
};
34+
35+
class Solution {
36+
public:
37+
int removeStones(vector<vector<int>>& stones) {
38+
int m = 10001;
39+
UnionFind uf(m << 1);
40+
for (auto& st : stones) {
41+
uf.unite(st[0], st[1] + m);
42+
}
43+
unordered_set<int> s;
44+
for (auto& st : stones) {
45+
s.insert(uf.find(st[0]));
46+
}
47+
return stones.size() - s.size();
48+
}
49+
};
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,48 @@
1+
type unionFind struct {
2+
p, size []int
3+
}
4+
5+
func newUnionFind(n int) *unionFind {
6+
p := make([]int, n)
7+
size := make([]int, n)
8+
for i := range p {
9+
p[i] = i
10+
size[i] = 1
11+
}
12+
return &unionFind{p, size}
13+
}
14+
15+
func (uf *unionFind) find(x int) int {
16+
if uf.p[x] != x {
17+
uf.p[x] = uf.find(uf.p[x])
18+
}
19+
return uf.p[x]
20+
}
21+
22+
func (uf *unionFind) union(a, b int) bool {
23+
pa, pb := uf.find(a), uf.find(b)
24+
if pa == pb {
25+
return false
26+
}
27+
if uf.size[pa] > uf.size[pb] {
28+
uf.p[pb] = pa
29+
uf.size[pa] += uf.size[pb]
30+
} else {
31+
uf.p[pa] = pb
32+
uf.size[pb] += uf.size[pa]
33+
}
34+
return true
35+
}
36+
37+
func removeStones(stones [][]int) (ans int) {
38+
m := 10001
39+
uf := newUnionFind(m << 1)
40+
for _, st := range stones {
41+
uf.union(st[0], st[1]+m)
42+
}
43+
s := map[int]bool{}
44+
for _, st := range stones {
45+
s[uf.find(st[0])] = true
46+
}
47+
return len(stones) - len(s)
48+
}

0 commit comments

Comments
 (0)