On a 2D plane, we place n stones at some integer coordinate points. Each coordinate point may have at most one stone.
A stone can be removed if it shares either the same row or the same column as another stone that has not been removed.
Given an array stones of length n where stones[i] = [xi, yi] represents the location of the ith stone, return the largest possible number of stones that can be removed.
classSolution { unordered_map<int, int> group; int island = 0; intfind(int n){ if(!group.count(n)) group[n] = n, island++; //if there is no island, add island if(n != group[n]) group[n] = find(group[n]); return group[n]; } voidmerge(int a, int b){ int pa = find(a), pb = find(b); if(pa != pb) group[pa] = pb, island--; //merge two islands and remove 1 } public: intremoveStones(vector<vector<int>>& stones){ int n = stones.size(); for(int i = 0; i < n; i ++) { merge(stones[i][0], ~stones[i][1]); } return n - island; } };