Merge Triplets to Form Target Triplet
MediumNeetCode 150 GreedyProblem
You get a list of triplets of integers and a target triplet. You may pick any two triplets and
merge them: the merged triplet takes the larger value in each position, and it replaces the
second one you picked. You may repeat this as often as you like.
Return true if some triplet in the list can end up exactly equal to target,
otherwise false.
Constraints
- 1 ≤ triplets.length ≤ 100000
- triplets[i].length = 3 and target.length = 3
- 1 ≤ every value ≤ 1000
Examples: pick one to animate
Solutions
Time: Each triplet is looked at once with three comparisons.
Space: Only three flags are stored.
Merging only ever raises values, so any triplet with a slot above the target is useless. Among the rest, merging them all gives the slot-wise maximum, which never exceeds the target. So the target is reachable exactly when each of the three target values appears in the matching slot of some usable triplet.