-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Create 0947_Most_Stones_Removed_with_Same_Row_or_Column.cpp
- Loading branch information
1 parent
f6a7600
commit 2fc6c75
Showing
1 changed file
with
55 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); | ||
} | ||
}; |