Skip to content

Commit 8590e6a

Browse files
authored
Create 947-most-stones-removed-with-same-row-or-column.js
1 parent 4aeb249 commit 8590e6a

File tree

1 file changed

+32
-0
lines changed

1 file changed

+32
-0
lines changed
Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
/**
2+
* @param {number[][]} stones
3+
* @return {number}
4+
*/
5+
const removeStones = function(stones) {
6+
const f = new Map()
7+
let islands = 0
8+
for (let i = 0; i < stones.length; i++) {
9+
union(stones[i][0], ~stones[i][1]) // row, col
10+
}
11+
return stones.length - islands
12+
13+
function find(x) {
14+
if (!f.has(x)) {
15+
islands++
16+
f.set(x, x)
17+
}
18+
if (x != f.get(x)) {
19+
f.set(x, find(f.get(x)))
20+
}
21+
return f.get(x)
22+
}
23+
24+
function union(x, y) {
25+
x = find(x)
26+
y = find(y)
27+
if (x !== y) {
28+
f.set(x, y)
29+
islands--
30+
}
31+
}
32+
}

0 commit comments

Comments
 (0)