You are given n stones placed on a 2D grid at integer coordinates.
Each coordinate can contain at most one stone.
A stone can be removed if it shares either the same row or the same column with another stone that has not yet been removed.
Your task is to determine the maximum number of stones that can be removed while ensuring at least one stone remains in each connected group.
💡 Intuition
Each connected group of stones (connected by rows or columns) forms a component.
From a component of size k, we can remove k - 1 stones and leave one behind.
Hence:
Maximum stones removed = Total stones - Number of connected components
Input: stones = [[0,0],[0,2],[1,0],[1,3],[2,2],[3,3]]
Output: 5
Input: stones = [[0,1],[1,2],[2,1],[2,3],[3,2],[3,0]]
Output: 4
Accepted:
Submission: