Skip to content

Commit 8729910

Browse files
authored
Create 947. Most Stones Removed with Same Row or Column (#570)
2 parents c3df2c6 + baf18ab commit 8729910

File tree

1 file changed

+36
-0
lines changed

1 file changed

+36
-0
lines changed
Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
class Solution {
2+
private:
3+
int bfs(vector<vector<int>>& stones, int start_index, vector<bool>& visited, int n) {
4+
queue<int> q;
5+
q.push(start_index);
6+
visited[start_index] = true;
7+
int count = 0;
8+
while (!q.empty()) {
9+
int index = q.front();
10+
q.pop();
11+
for (int i = 0; i < n; i++) {
12+
if(!visited[i]){
13+
if((stones[i][0] == stones[index][0] || stones[i][1] == stones[index][1])){
14+
visited[i] = true;
15+
q.push(i);
16+
count++;
17+
}
18+
}
19+
}
20+
}
21+
return count;
22+
}
23+
24+
public:
25+
int removeStones(vector<vector<int>>& stones) {
26+
int n = stones.size();
27+
vector<bool> visited(n, 0);
28+
int ans = 0;
29+
for (int i = 0; i < n; i++) {
30+
if (!visited[i]) {
31+
ans += bfs(stones, i, visited, n);
32+
}
33+
}
34+
return ans;
35+
}
36+
};

0 commit comments

Comments
 (0)