Generation

generate functionMon, 14 Nov 2022

On a 2D plane, we place n stones at some integer coordinate points. Each coordinate point may have at most one stone. A stone can be removed if it shares either the same row or the same column as another stone that has not been removed. Given an array stones of length n where stones[i] = [xi, yi] represents the location of the ith stone, return the largest possible number of stones that can be removed.

def removeStones(stones): s = {(i, j) for i, j in stones} n = len(stones) UF = {i: i for i in range(n)} def find(x): if UF[x] != x: UF[x] = find(UF[x]) return UF[x] def union(x, y): UF.update({find(x): find(y)}) for x, y in s: for i in (x, 10000): for j in (y, 10000): if (i, j) in s: union(x * 10000 + y, i * 10000 + j) return n - len({find(x) for x in UF})

Questions about programming?Chat with your personal AI assistant