Skip to content

Commit d2ccf08

Browse files
committed
Add solution #947
1 parent 3c1280d commit d2ccf08

File tree

2 files changed

+63
-1
lines changed

2 files changed

+63
-1
lines changed

README.md

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
# 1,031 LeetCode solutions in JavaScript
1+
# 1,032 LeetCode solutions in JavaScript
22

33
[https://leetcode.com/](https://leetcode.com/)
44

@@ -756,6 +756,7 @@
756756
944|[Delete Columns to Make Sorted](./solutions/0944-delete-columns-to-make-sorted.js)|Easy|
757757
945|[Minimum Increment to Make Array Unique](./solutions/0945-minimum-increment-to-make-array-unique.js)|Medium|
758758
946|[Validate Stack Sequences](./solutions/0946-validate-stack-sequences.js)|Medium|
759+
947|[Most Stones Removed with Same Row or Column](./solutions/0947-most-stones-removed-with-same-row-or-column.js)|Medium|
759760
966|[Vowel Spellchecker](./solutions/0966-vowel-spellchecker.js)|Medium|
760761
970|[Powerful Integers](./solutions/0970-powerful-integers.js)|Easy|
761762
976|[Largest Perimeter Triangle](./solutions/0976-largest-perimeter-triangle.js)|Easy|
Lines changed: 61 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,61 @@
1+
/**
2+
* 947. Most Stones Removed with Same Row or Column
3+
* https://leetcode.com/problems/most-stones-removed-with-same-row-or-column/
4+
* Difficulty: Medium
5+
*
6+
* On a 2D plane, we place n stones at some integer coordinate points. Each coordinate point may
7+
* have at most one stone.
8+
*
9+
* A stone can be removed if it shares either the same row or the same column as another stone
10+
* that has not been removed.
11+
*
12+
* Given an array stones of length n where stones[i] = [xi, yi] represents the location of the
13+
* ith stone, return the largest possible number of stones that can be removed.
14+
*/
15+
16+
/**
17+
* @param {number[][]} stones
18+
* @return {number}
19+
*/
20+
var removeStones = function(stones) {
21+
const parent = new Map();
22+
const find = (stoneIndex) => {
23+
if (!parent.has(stoneIndex)) {
24+
parent.set(stoneIndex, stoneIndex);
25+
}
26+
if (parent.get(stoneIndex) !== stoneIndex) {
27+
parent.set(stoneIndex, find(parent.get(stoneIndex)));
28+
}
29+
return parent.get(stoneIndex);
30+
};
31+
32+
const union = (stone1, stone2) => {
33+
parent.set(find(stone1), find(stone2));
34+
};
35+
36+
const stoneMap = new Map();
37+
for (let i = 0; i < stones.length; i++) {
38+
const [row, col] = stones[i];
39+
const rowKey = `r${row}`;
40+
const colKey = `c${col}`;
41+
42+
if (stoneMap.has(rowKey)) {
43+
union(i, stoneMap.get(rowKey));
44+
} else {
45+
stoneMap.set(rowKey, i);
46+
}
47+
48+
if (stoneMap.has(colKey)) {
49+
union(i, stoneMap.get(colKey));
50+
} else {
51+
stoneMap.set(colKey, i);
52+
}
53+
}
54+
55+
const uniqueGroups = new Set();
56+
for (let i = 0; i < stones.length; i++) {
57+
uniqueGroups.add(find(i));
58+
}
59+
60+
return stones.length - uniqueGroups.size;
61+
};

0 commit comments

Comments
 (0)