Minimum Generations to Target Point Problem
Description
LeetCode Problem 3923.
You are given a 2D integer array points where points[i] = [x_i, y_i, z_i] represents a point in 3D space, and an integer array target representing a target point.
Define generation 0 as the initial list of points. For each integer k >= 1, form generation k as follows:
- Consider every pair of two distinct points
a = [x_1, y_1, z_1]andb = [x_2, y_2, z_2]taken from all points produced in generations 0 throughk - 1. - For each such pair, compute
c = [floor((x_1 + x_2) / 2), floor((y_1 + y_2) / 2), floor((z_1 + z_2) / 2)]and collect every suchcinto a generationk. - All points in the generation
kare produced simultaneously from points in generations 0 throughk - 1. - After generation
kis formed, the points in the generationkare considered available for forming later generations.
Return the smallest integer k such that the target appears in one of the generations 0 through k. If the target is already in the initial points, return 0. If it is impossible to obtain the target, return -1.
Notes:
- floor denotes rounding down to the nearest integer.
- "Two distinct points" means the two chosen points must have different
(x, y, z)coordinates. A point cannot be paired with itself, and pairing two points with identical coordinates is not possible.
Example 1:
Input: points = [[0,0,0],[6,6,6]], target = [3,3,3]
Output: 1
Explanation:
- Generation 0: The initial points = [[0, 0, 0], [6, 6, 6]].
- The target = [3, 3, 3] does not exist in generation 0.
- Generation 1: For each pair of points in generation 0, we create new points.
- Using [0, 0, 0] and [6, 6, 6], we generate [3, 3, 3].
- After generation 1, points = [[0, 0, 0], [6, 6, 6], [3, 3, 3]].
- The target = [3, 3, 3] is found in generation 1, so the smallest k is 1.
Example 2:
Input: points = [[0,0,0],[5,5,5]], target = [1,1,1]
Output: 2
Explanation:
- Generation 0: The initial points = [[0, 0, 0], [5, 5, 5]].
- The target = [1, 1, 1] does not exist in generation 0.
- Generation 1: For each pair of points in generation 0, we create new points.
- Using [0, 0, 0] and [5, 5, 5], we generate [2, 2, 2].
- After generation 1, points = [[0, 0, 0], [5, 5, 5], [2, 2, 2]].
- Generation 2: For each pair of points available after generation 1, we create new points.
- Using [0, 0, 0] and [5, 5, 5], we generate [2, 2, 2].
- Using [0, 0, 0] and [2, 2, 2], we generate [1, 1, 1].
- Using [5, 5, 5] and [2, 2, 2], we generate [3, 3, 3].
- After generation 2, points = [[0, 0, 0], [5, 5, 5], [2, 2, 2], [1, 1, 1], [3, 3, 3]].
- The target = [1, 1, 1] is found in generation 2, so the smallest k is 2.
Example 3:
Input: points = [[0,0,0],[2,2,2],[3,3,3]], target = [2,2,2]
Output: 0
Explanation:
- Generation 0: The initial points = [[0, 0, 0], [2, 2, 2], [3, 3, 3]].
- The target = [2, 2, 2] already exists in generation 0, so the smallest k is 0.
Example 4:
Input: points = [[1,2,3]], target = [5,5,5]
Output: -1
Explanation:
- Only one initial point is available, so no new points can be generated.
- Therefore, the target cannot be obtained, and the answer is -1.
Constraints:
1 <= points.length <= 20points[i] = [x_i, y_i, z_i]0 <= x_i, y_i, z_i <= 6target.length == 30 <= target[i] <= 6- The initial set of points contains no duplicates.
Solution
Approach: Simulate Generation by Generation
All coordinates stay between 0 and 6, since the floor of an average of two values in that range stays in it. So there are at most 7 * 7 * 7 = 343 distinct points, and we can keep them in a boolean table.
If the target is already present, the answer is 0. Otherwise repeat: take all points found so far, compute the midpoint (with floor) of every pair of distinct points, and add the ones that are new. If a round adds nothing, no later round can either, so the target is unreachable and the answer is -1. Otherwise the first round after which the target is present is the answer.
Because generation k is built from all points of generations 0..k-1, using the set of all points found so far is exactly what the statement describes.
Complexity: each round costs O(P2) for P <= 343 points, and a new point appears in every round that continues, so there are at most 343 rounds. The real number of rounds is much smaller in practice.
class Solution:
def minGenerations(self, points: List[List[int]], target: List[int]) -> int:
cur = set(map(tuple, points))
goal = tuple(target)
if goal in cur:
return 0
k = 0
while True:
k += 1
pts = list(cur)
new = set()
for i in range(len(pts)):
a = pts[i]
for j in range(i + 1, len(pts)):
b = pts[j]
new.add(((a[0] + b[0]) // 2, (a[1] + b[1]) // 2, (a[2] + b[2]) // 2))
if new <= cur:
return -1
cur |= new
if goal in cur:
return k