diff --git a/0947_Most_Stones_Removed_with_Same_Row_or_Column.cpp b/0947_Most_Stones_Removed_with_Same_Row_or_Column.cpp new file mode 100644 index 0000000..b402e06 --- /dev/null +++ b/0947_Most_Stones_Removed_with_Same_Row_or_Column.cpp @@ -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>& stones) { + int n = stones.size(); + vector parent(n, -1); + function find = [&](int u){ + while(parent[u]>0){ + u=parent[u]; + } + return u; + }; + function 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