Most Stones Removed with Same Row or Column
On a 2D plane there are n stones, each at an integer coordinate with at most one stone per point. A stone can be removed if it shares its row or its column with another stone that has not yet been removed. Return the largest number of stones you can remove.
Open official problem prompt ↗Compute the maximum removable stones, which equals total stones minus the number of connected components formed by shared rows and columns.
Picture threads tying stones that share a line. As long as a stone is still tied to a neighbor, you can lift it off. You can keep lifting until each cluster has just one anchor stone left.
- Input
- stones = [[0,0],[0,1],[1,0],[1,2],[2,1],[2,2]]
- Output
- 5
- Why
- All six stones form one connected component through shared rows/columns, so you can remove all but one, leaving 6 - 1 = 5 removed.
1 <= stones.length <= 10000 <= xi, yi <= 10^4No two stones are at the same coordinate point