 All Problems
Most Stones Removed with Same Row or Column
medium
union-find
graph
dfs
facebook
amazon

On a 2D plane, there are n stones at positions stones[i] = [xi, yi]. A stone can be removed if it shares a row or column with another stone.

Return the largest possible number of stones that can be removed.

Example 1:

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

Example 2:

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

Constraints:

  • 1 <= stones.length <= 1000
  • 0 <= xi, yi <= 10^4
Run to check your code against the sample cases, or submit to run every case