Most Stones Removed with Same Row or Column

Medium

Description

Given stones on a 2D plane, remove stones that share row or column with another stone. Return max stones that can be removed.

Examples

Input:stones = [[0,0],[0,1],[1,0],[1,2],[2,1],[2,2]]
Output:5
Explanation:

All but one can be removed.

Input:stones = [[0,0],[1,1],[2,2],[3,3]]
Output:0
Explanation:

No two stones share the same row or column, so no stones can be removed.

Input:stones = [[0,1],[1,0],[1,1],[2,1],[2,2]]
Output:4
Explanation:

All stones are connected through shared rows or columns, forming one connected component. It is possible to remove 4 stones, leaving 1 stone remaining.

Constraints

  • 1 ≤ stones.length ≤ 1000

Ready to solve this problem?

Practice solo or challenge other developers in a real-time coding battle!