Maximum Points Activated with One Addition Problem
Description
LeetCode Problem 3873.
You are given a 2D integer array points, where points[i] = [x_i, y_i] represents the coordinates of the i^th point. All coordinates in points are distinct.
If a point is activated, then all points that have the same x-coordinate or y-coordinate become activated as well.
Activation continues until no additional points can be activated.
You may add one additional point at any integer coordinate (x, y) not already present in points. Activation begins by activating this newly added point.
Return an integer denoting the maximum number of points that can be activated, including the newly added point.
Example 1:
Input: points = [[1,1],[1,2],[2,2]]
Output: 4
Explanation:
Adding and activating a point such as (1, 3) causes activations:
- (1, 3) shares x = 1 with (1, 1) and (1, 2) -> (1, 1) and (1, 2) become activated.
- (1, 2) shares y = 2 with (2, 2) -> (2, 2) becomes activated.
Thus, the activated points are (1, 3), (1, 1), (1, 2), (2, 2), so 4 points in total. We can show this is the maximum activated.
Example 2:
Input: points = [[2,2],[1,1],[3,3]]
Output: 3
Explanation:
Adding and activating a point such as (1, 2) causes activations:
- (1, 2) shares x = 1 with (1, 1) -> (1, 1) becomes activated.
- (1, 2) shares y = 2 with (2, 2) -> (2, 2) becomes activated.
Thus, the activated points are (1, 2), (1, 1), (2, 2), so 3 points in total. We can show this is the maximum activated.
Example 3:
Input: points = [[2,3],[2,2],[1,1],[4,5]]
Output: 4
Explanation:
Adding and activating a point such as (2, 1) causes activations:
- (2, 1) shares x = 2 with (2, 3) and (2, 2) -> (2, 3) and (2, 2) become activated.
- (2, 1) shares y = 1 with (1, 1) -> (1, 1) becomes activated.
Thus, the activated points are (2, 1), (2, 3), (2, 2), (1, 1), so 4 points in total.
Constraints:
1 <= points.length <= 10^5points[i] = [x_i, y_i]-10^9 <= x_i, y_i <= 10^9pointscontains all distinct coordinates.
Solution
Approach: Union-Find on Coordinates
Treat every x value and every y value as a node, and each point (x, y) as an edge joining x and y. Points that activate each other are exactly the edges of one connected component. Shift the y values by a large constant (3 * 10^9) so that x and y nodes never collide.
A new point (a, b) joins the component containing a (if any) with the one containing b (if any), and can be chosen with a and b in any two different components. So the best answer is the sizes (in points) of the two largest components plus the new point itself.
Complexity: O(n * α(n)) time and O(n) space, where α is the inverse Ackermann function.
class UnionFind:
def __init__(self):
self.p = {}
self.size = {}
def find(self, x):
if x not in self.p:
self.p[x] = x
self.size[x] = 1
if self.p[x] != x:
self.p[x] = self.find(self.p[x])
return self.p[x]
def union(self, a, b):
pa, pb = self.find(a), self.find(b)
if pa == pb:
return False
if self.size[pa] > self.size[pb]:
self.p[pb] = pa
self.size[pa] += self.size[pb]
else:
self.p[pa] = pb
self.size[pb] += self.size[pa]
return True
class Solution:
def maxActivated(self, points: List[List[int]]) -> int:
uf = UnionFind()
m = int(3e9)
for x, y in points:
uf.union(x, y + m)
cnt = Counter()
for x, _ in points:
cnt[uf.find(x)] += 1
mx1 = mx2 = 0
for x in cnt.values():
if mx1 < x:
mx2 = mx1
mx1 = x
elif mx2 < x:
mx2 = x
return mx1 + mx2 + 1