-
Notifications
You must be signed in to change notification settings - Fork 23
/
Copy path0947-most-stones-removed-with-same-row-or-column.js
61 lines (54 loc) · 1.62 KB
/
0947-most-stones-removed-with-same-row-or-column.js
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
/**
* 947. Most Stones Removed with Same Row or Column
* https://leetcode.com/problems/most-stones-removed-with-same-row-or-column/
* Difficulty: Medium
*
* 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.
*/
/**
* @param {number[][]} stones
* @return {number}
*/
var removeStones = function(stones) {
const parent = new Map();
const find = (stoneIndex) => {
if (!parent.has(stoneIndex)) {
parent.set(stoneIndex, stoneIndex);
}
if (parent.get(stoneIndex) !== stoneIndex) {
parent.set(stoneIndex, find(parent.get(stoneIndex)));
}
return parent.get(stoneIndex);
};
const union = (stone1, stone2) => {
parent.set(find(stone1), find(stone2));
};
const stoneMap = new Map();
for (let i = 0; i < stones.length; i++) {
const [row, col] = stones[i];
const rowKey = `r${row}`;
const colKey = `c${col}`;
if (stoneMap.has(rowKey)) {
union(i, stoneMap.get(rowKey));
} else {
stoneMap.set(rowKey, i);
}
if (stoneMap.has(colKey)) {
union(i, stoneMap.get(colKey));
} else {
stoneMap.set(colKey, i);
}
}
const uniqueGroups = new Set();
for (let i = 0; i < stones.length; i++) {
uniqueGroups.add(find(i));
}
return stones.length - uniqueGroups.size;
};