Most Stones Removed with Same Row or Column

You are given distinct stones placed at integer coordinates. You may remove a stone only when another remaining stone shares its row or its column. Return the…

You are given distinct stones placed at integer coordinates. You may remove a stone only when another remaining stone shares its row or its column. Return the maximum number of stones that can be removed. Sharing coordinates across different axes does not count; row 1 and column 1 are different lines.

- 1 <= stones.length <= 1000 - Each stone contains exactly two integer coordinates: [row, column] - 0 <= row, column <= 10000 - No two stones occupy the same coordinate pair - Preset visualization uses at most 8 stones; larger valid inputs are execution-only

Example 1: input stones = [[1,4],[1,9],[6,9],[8,2]]; output 2. Remove [1,4] while [1,9] remains in its row, then remove [1,9] while [6,9] remains in its column. The connected group keeps one survivor; [8,2] is isolated and cannot be removed.

Example 2: input stones = [[1,2],[2,1]]; output 0. Neither stone shares a row or a column with the other. Matching a row number to the other stone's column number does not make a removal legal.

Category: Graphs. Topic: graph. Difficulty: Medium. Patterns: graph-traversal.