LeetCode 947 Most Stones Removed with Same Row or Column - Example and Complexity Analysis

Walkthrough and analysis

Trace [[0,0],[0,2],[1,1],[2,0],[2,2]].

Stone 0: (0,0). Stone 1: (0,2). Stone 2: (1,1). Stone 3: (2,0). Stone 4: (2,2).

Stones 0 and 1 share row 0. Union them.

Stones 0 and 3 share column 0. Union them (0,1,3 now connected).

Stones 1 and 4 share column 2. Union them (0,1,3,4 now connected).

Stone 2 at (1,1) shares nothing with others. Alone.

Components: {0,1,3,4}, {2}. Count: 22.

Removals: 52=35 - 2 = 3.

O(n2α(n))O(n^2 \cdot \alpha(n)) time comparing all pairs. O(n)O(n) space.