<jupyter_start><jupyter_text>说明：    在2D平面上，我们将石头放置在一些整数坐标点上。    每个坐标点最多可以有一个石头。    现在，a move 包括删除一块与网格上另一块石头共享同一列或一行的石头。    我们可以进行的最大动作数是多少？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: 3Example 3:    Input: stones = [[0,0]]    Output: 0Note:    1、1 <= stones.length <= 1000    2、0 <= stones[i][j] < 10000 将同行或者同列的石头是做一个 Group, 这个Group中能够 move 掉最多的石头的数量就是 len(group) - 1 个<jupyter_code>from collections import defaultdict
class Solution:
    def removeStones(self, stones) -> int:
        
        def find(x):
            if parent[x] != x:
                parent[x] = find(parent[x])
            return parent[x]
        
        def union(x, y):
            root_x = find(x)
            root_y = find(y)
            if root_x == root_y:
                return
            else:
                parent[root_x] = root_y
                
        
        N = 10000 #　将二维的坐标转换为 1 个数字
        
        row_parent = defaultdict(list)
        col_parent = defaultdict(list)
        parent = {}
        for idx, item in enumerate(stones):
            ids = item[0] * N + item[1]
            row_parent[item[0]].append(ids)
            col_parent[item[1]].append(ids)
            parent[ids] = ids
        
        print(row_parent)
        for item in row_parent.items():
            id0 = item[1][0]
            for val in item[1]:
                if find(val) != find(id0):
                    union(val, id0)
        
        for item in col_parent.items():
            id0 = item[1][0]
            for val in item[1]:
                if find(val) != find(id0):
                    union(val, id0)
        print(col_parent)
        
        root_set = set()
        for val in parent:
            root = find(val)
            root_set.add(root)
        return len(stones) - len(root_set)
stones_ = [[0,0],[0,1],[1,0],[1,2],[2,1],[2,2]]
solution = Solution()
solution.removeStones(stones_)<jupyter_output><empty_output>
{'fraction_non_alphanumeric': 0.13517513566847558, 'fraction_numerical': 0.038973852984706465, 'mean_word_length': 1.7591836734693878, 'pattern_counts': {'":': 0, '<': 9, '<?xml version=': 0, '>': 6, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'chain_length': '1', 'dir': 'jupyter-structured-clean-dedup', 'id': '545028', 'license': 'Apache-2.0', 'n_tokens_mistral': 796, 'n_tokens_neox': 773, 'n_words': 144, 'path': 'Union Fold/0912/947. Most Stones Removed with Same Row or Column.ipynb', 'repo_name': 'YuHe0108/Leetcode', 'size': '2027'}