1112. Max Stones Removed with Same Row or Column

HardGraphsDSUGraph

On a 2D plane there are stones at given integer coordinates. A stone can be removed if it shares its row or column with another remaining stone. Return the maximum number of stones that can be removed. The input is JSON {stones}.

Input: JSON {stones}.

Output: Integer — the maximum stones removable.

Examples

Example 1
Input: {"stones":[[0,0],[0,1],[1,0],[1,2],[2,1],[2,2]]}
Output: 5
Explanation: All but one can be removed.
Example 2
Input: {"stones":[[0,0],[0,2],[1,1],[2,0],[2,2]]}
Output: 3
Explanation: Two components leave two stones.
Example 3
Input: {"stones":[[0,0]]}
Output: 0
Explanation: A lone stone cannot be removed.

Constraints

Asked by

AmazonGoogleMicrosoftMetaAdobe
Solve this problem in the editor →