-
Notifications
You must be signed in to change notification settings - Fork 119
/
Copy path0947-MostStonesRemovedWithSameRoworColumn.cs
52 lines (43 loc) · 1.34 KB
/
0947-MostStonesRemovedWithSameRoworColumn.cs
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
//-----------------------------------------------------------------------------
// Runtime: 140ms
// Memory Usage:
// Link:
//-----------------------------------------------------------------------------
using System.Collections.Generic;
namespace LeetCode
{
public class _0947_MostStonesRemovedWithSameRoworColumn
{
private const int BOARD_SIZE = 10000;
public int RemoveStones(int[][] stones)
{
var uf = new UnionFind(BOARD_SIZE * 2);
foreach (var stone in stones)
uf.Union(stone[0], stone[1] + BOARD_SIZE);
var set = new HashSet<int>();
foreach (var stone in stones)
set.Add(uf.FindParent(stone[0]));
return stones.Length - set.Count;
}
public class UnionFind
{
private int[] parents;
public UnionFind(int size)
{
parents = new int[size];
for (int i = 0; i < size; i++)
parents[i] = i;
}
public int FindParent(int x)
{
while (parents[x] != x)
x = parents[x];
return parents[x];
}
public void Union(int x, int y)
{
parents[FindParent(x)] = FindParent(y);
}
}
}
}