Skip to content

Commit

Permalink
Create 0947_Most_Stones_Removed_with_Same_Row_or_Column.cpp
Browse files Browse the repository at this point in the history
  • Loading branch information
Nothing-avil authored Aug 29, 2024
1 parent f6a7600 commit 2fc6c75
Showing 1 changed file with 55 additions and 0 deletions.
55 changes: 55 additions & 0 deletions 0947_Most_Stones_Removed_with_Same_Row_or_Column.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,55 @@
// ███████╗ █████╗ ███╗ ██╗ ██████╗ █████╗ ██████╗ ██████╗ ██╗ ██╗
// ██╔════╝ ██╔══██╗ ████╗ ██║ ██╔══██╗ ██╔══██╗ ██╔══██╗ ██╔══██╗ ██║ ██║
// ███████╗ ███████║ ██╔██╗ ██║ ██║ ██║ ███████║ ██████╔╝ ██████╔╝ ███████║
// ╚════██║ ██╔══██║ ██║╚██╗██║ ██║ ██║ ██╔══██║ ██╔═██╗ ██╔══██╗ ██╔══██║
// ███████║ ██║ ██║ ██║ ╚████║ ██████╔╝ ██║ ██║ ██║ ██╗ ██████╔╝ ██║ ██║
// ╚══════╝ ╚═╝ ╚═╝ ╚═╝ ╚═══╝ ╚═════╝ ╚═╝ ╚═╝ ╚═╝ ╚═╝ ╚═════╝ ╚═╝ ╚═╝
#pragma GCC optimize("Ofast", "inline", "ffast-math", "unroll-loops","no-stack-protector")
#pragma GCC target("sse,sse2,sse3,ssse3,sse4,popcnt,abm,mmx,avx,avx2,tune=native", "f16c")
auto init = []() {
ios::sync_with_stdio(false);
cin.tie(nullptr);
cout.tie(nullptr);
return 'c';
}();
class Solution {
public:
int removeStones(vector<vector<int>>& stones) {
int n = stones.size();
vector<int> parent(n, -1);
function<int(int)> find = [&](int u){
while(parent[u]>0){
u=parent[u];
}
return u;
};
function<void(int, int)> unions = [&](int a, int b){
int i = find(a);
int j = find(b);
if(i!=j){
if(parent[i] > parent[j]){
parent[i]+= parent[j];
parent[j] = i;
}
else{
parent[j] += parent[i];
parent[i]=j;
}
}
};
for(int i=0 ; i<n ; i++){
for(int j= i+1; j<n; j++){
if(stones[i][0]==stones[j][0] || stones[i][1]==stones[j][1]){
unions(i,j);
}
}
}
int c=0;
for(int i=0; i<n; i++){
if(parent[i]<0){
c++;
}
}
return abs(n-c);
}
};

0 comments on commit 2fc6c75

Please sign in to comment.