Removing Stones From a Shared Yard

Implement maxRemovable

Some cells of a grid each hold one stone. You may lift a stone off the grid only if another stone is still standing somewhere in the same row or the same column. Work out the greatest number of stones that can be lifted off, one after another.

Stones that share a row or a column form groups, and in every group all stones but one can be removed. Union-find on the rows and columns (instead of on the stones) counts the groups in one pass.

Example 1:

Input: stones = [[1,1],[1,4],[3,4],[3,1],[6,2],[6,8],[7,8],[5,5]]

Output: 5

Example 2:

Input: stones = [[0,0]]

Output: 0

Example 3:

Input: stones = [[0,0],[0,1],[1,1],[1,0]]

Output: 3

+ 14 hidden test cases run on Submit.

Constraints:

  • ●1 ≤ stones.length ≤ 10; stones[i] = [row, column] with 0 ≤ row, column ≤ 9; all stones are at different positions
  • ●A stone may be lifted off only while some other stone is still on the grid in its row or in its column
  • ●Stones are removed one at a time; the same stone cannot be removed twice
  • ●Return the largest number of stones that can be removed

stones =

[[1,1], [1,4], [3,4], [3,1], [6,2], [6,8], [7,8], [5,5]]