Skip to content
BytePatterns

Stones Sharing A Line

MediumUnion-Find#union-find#connected-components~30m

Problem

Stones sit on distinct points of an integer grid. You may take a stone off the board if at least one other stone still on the board shares its row or its column. Return the largest number of stones you can take off by choosing the order well.

Examples

Input:  stones = [[0, 0], [0, 1], [1, 0], [1, 2], [2, 1], [2, 2]]
Output: 5
Why:    all six stones are linked through shared rows and columns, so one remains
Input:  stones = [[0, 0], [0, 2], [1, 1], [2, 0], [2, 2]]
Output: 3
Why:    the centre stone shares nothing, and the four corners reduce to one
Input:  stones = [[0, 0]]
Output: 0
Why:    edge case, a lone stone has no partner

Hints

0 / 3

Stuck on the idea rather than the code? Union by Rank or Size covers it.