LeetCode 947 Most Stones Removed with Same Row or Column - Implementation

The approach

Union stones sharing row or column. Answer = n - components.

def removeStones(stones): parent = list(range(len(stones)))

def find(x):
    if parent[x] != x:
        parent[x] = find(parent[x])
    return parent[x]

def union(x, y):
    parent[find(x)] = find(y)

for i in range(len(stones)):
    for j in range(i + 1, len(stones)):
        if stones[i][0] == stones[j][0] or stones[i][1] == stones[j][1]:
            union(i, j)

components = len(set(find(i) for i in range(len(stones))))
return len(stones) - components

O(n2α(n))O(n^2 \cdot \alpha(n)) time, O(n)O(n) space.