Search⌘ K
AI Features

Solution: Most Stones Removed with Same Row or Column

Understand how to apply the Union Find pattern to group stones sharing the same row or column, then calculate the maximum stones removable by subtracting the number of connected groups from the total stones. This lesson guides you through an optimized approach with time complexity O(n) and explains implementation details like union and find methods.

Statement

Given an array of nn stones in a two-dimensional plane, where each stone is represented by a pair of x and y coordinates, find the maximum number of stones we can remove with the following condition:

A stone can be removed if it shares either the same row or the same column with another stone that has not been removed so far.

Stones are provided as an array, stones, of length nn, where stones[i]=[xi,yi]stones[i] = [x_i, y_i] ...