File tree Expand file tree Collapse file tree 1 file changed +37
-0
lines changed
Expand file tree Collapse file tree 1 file changed +37
-0
lines changed Original file line number Diff line number Diff line change 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+ };
You can’t perform that action at this time.
0 commit comments