Example 1
- Input
- triplets = [[2,5,3],[1,8,4],[1,7,5]], target = [2,7,5]
- Output
- true
Merge [2,5,3] into [1,7,5] to get [max(2,1), max(5,7), max(3,5)] = [2,7,5], which equals the target.
You are given a list of integer triplets and a target triplet, and you may repeatedly replace any triplet with the element-wise maximum of two triplets. Return whether any sequence of such merges can produce the target triplet.
You are given a 2D integer array triplets where triplets[i] = [a, b, c] is the ith triplet, and an integer array target = [x, y, z] describing the triplet you want to obtain.
In one operation you may pick two indices i and j with i != j and replace triplets[j] with [max(a_i, a_j), max(b_i, b_j), max(c_i, c_j)]. For example, merging [2, 5, 3] into [1, 7, 5] gives [max(2, 1), max(5, 7), max(3, 5)] = [2, 7, 5]. Return true if the target triplet can ever appear in triplets, otherwise return false.
1 <= triplets.length <= 10^5triplets[i].length == target.length == 31 <= a, b, c, x, y, z <= 1000Merge [2,5,3] into [1,7,5] to get [max(2,1), max(5,7), max(3,5)] = [2,7,5], which equals the target.
No triplet holds 2 in the middle position, so the target can never be formed.
Merge [2,5,3] into [1,2,5] to get [2,5,5], then merge [2,5,5] into [5,2,3] to get [5,5,5].
The only row is already the target, so zero operations suffice. Two distinct indices are needed only when actually performing a merge.
The first row supplies 2 and 5 but also overshoots the middle coordinate. The only safe row [1,7,4] cannot supply either missing value, so checking coordinates without filtering would give a false positive.
A maximum can raise a coordinate but can never lower it. For target [2,7,5], row [1,8,4] is unusable because its middle 8 would survive every later merge. Safe rows [2,5,3] and [1,7,5] can be combined: one supplies the first coordinate 2, the other supplies 7 and 5. The useful question is whether safe rows collectively supply all three target values.
Reveal Subset Enumeration: intuition, complexity, and code
true without any merge taking place.2 in Example 2, makes the answer false no matter what is merged.target and calling it impossible.true for targets that can never actually form.When an operation only moves values in one direction, inputs that overshoot the goal are permanently useless: filter them out and the greedy choice becomes checking each coordinate independently.