Merge Triplets to Form Target looks intimidating because of the word "merge," but the whole problem collapses once you notice one fact: merging can only ever raise a value, never lower it. Once you see that, the answer is a single linear scan.
Problem. You have a list of triplets like [a, b, c], and a target triplet. You may repeatedly pick
two triplets and merge them: the result keeps the larger value in each of the three positions. Return
True if, after any number of merges, you can produce exactly the target.
Example: triplets = [[2, 5, 3], [1, 8, 4], [1, 7, 5]], target = [2, 7, 5] → True
(merge [2, 5, 3] and [1, 7, 5] to get [2, 7, 5]).
The slow way first
You could try every sequence of merges and see if any path reaches the target. But the number of merge orders explodes combinatorially — that search space is enormous and completely unnecessary.
The question to ask: what does a merge actually do to a single position? It takes the max of the two values there. Max never goes down. So a triplet can only ever help if all three of its values already fit under the target — otherwise it would push some position above the target, and there is no way to come back down.
The idea: keep good triplets, check the maxima
Walk the list once. A triplet is usable only if every value is <= target in its position. Throw away any triplet that overshoots anywhere. Among the usable triplets, the only thing that matters is whether, between them, they supply the target's exact value in each of the three positions. If position i of some usable triplet equals target[i], that maximum is covered. If all three positions get covered, merging the usable triplets gives exactly the target.
The key insight: a value above the target is poison, and the only thing a usable triplet can give us is an exact match at some position. We just need all three exact matches to appear somewhere.
Walk through it
Step through the animation. The target sits on top in purple. The pointer i scans each triplet. [2, 5, 3] fits everywhere and matches the target exactly at index 0 — covered. [1, 8, 4] has an 8 > 7, so it overshoots and gets discarded. [1, 7, 5] fits and matches at indices 1 and 2. The got set fills to {0, 1, 2} — all three maxima covered — so the answer is True.
Pseudocode
make an empty set called "got" # which target positions we can hit exactly
for each triplet [a, b, c]:
if a <= target[0] and b <= target[1] and c <= target[2]: # usable?
for each position i in 0, 1, 2:
if triplet[i] == target[i]:
add i to got # this triplet supplies target[i]
return whether got contains all three positions {0, 1, 2}The Python solution
def merge_triplets(triplets, target):
got = set()
for a, b, c in triplets:
if a <= target[0] and b <= target[1] and c <= target[2]:
for i in range(3):
if (a, b, c)[i] == target[i]:
got.add(i)
return len(got) == 3gotis a set of the target positions we have managed to match exactly.- We unpack each triplet into
a, b, cand loop once over the list. - Line 4 is the usability filter: every value must be
<= targetin its slot, or the triplet is poison and we skip it. - Lines 6 to 7 record each exact match: if a usable triplet equals the target at position
i, that maximum is covered. - We return
Trueonly when all three positions{0, 1, 2}are ingot.
Complexity
| Case | Time | Notes |
|---|---|---|
| Brute force (try merge orders) | exponential (moderate) | huge search space |
| Single scan (this solution) | O(n) (moderate) | n triplets, constant work each |
O(1) (fast)We do constant work per triplet (three comparisons, three equality checks) and the got set never holds more than three elements, so the extra space is O(1).
When this pattern shows up
When an operation is monotonic — it can only move a value one direction, like max or min or OR-ing
bits — you rarely need to simulate it. Ask what each operation can and cannot do to a single element, and the
problem often reduces to a one-pass filter plus a coverage check.
The filter must come first. If you record matches from a triplet that overshoots somewhere, you might count an exact match it can never actually contribute — because that triplet can never be merged in without ruining the position it overshoots.
Practice
For triplets = [[2, 5, 3], [1, 8, 4], [1, 7, 5]], target = [2, 7, 5], why is [1, 8, 4] discarded?
1. Why can a triplet with any value above the target never help?
2. What does the got set track?
3. Why must the usability filter run before recording matches?
4. What is the time complexity of the one-pass solution?