Skip to content

Commit af9d5f4

Browse files
authored
Create Maximum Stone Removal
1 parent bf50226 commit af9d5f4

File tree

1 file changed

+37
-0
lines changed

1 file changed

+37
-0
lines changed

Maximum Stone Removal

Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
class Solution {
2+
public:
3+
int maxRemove(vector<vector<int>>& stones) {
4+
int n = stones.size() , ans = 0 ;
5+
map<int , vector<int>> row , col ;
6+
for(int i = 0 ; i < n ; i ++ ){
7+
int r = stones[i][0] , c = stones[i][1] ;
8+
row[r].push_back(i) ;
9+
col[c].push_back(i) ;
10+
}
11+
vector<bool>vis(n , 0) ;
12+
for(int i = 0 ; i < n ; i ++){
13+
if(!vis[i]){
14+
ans ++ ;
15+
vis[i] = 1 ;
16+
queue<int> q ;
17+
q.push(i) ;
18+
while(!q.empty()){
19+
int temp = q.front() ;
20+
q.pop() ;
21+
int r = stones[temp][0] , c = stones[temp][1] ;
22+
for(int x : row[r]){
23+
if(!vis[x]) q.push(x) ;
24+
vis[x] = 1 ;
25+
}
26+
for(int y : col[c]){
27+
if(!vis[y]) q.push(y) ;
28+
vis[y] = 1 ;
29+
}
30+
row.erase(r) ;
31+
col.erase(c) ;
32+
}
33+
}
34+
}
35+
return n - ans ;
36+
}
37+
};

0 commit comments

Comments
 (0)