The merge operation is just component-wise max, so any triplet that overshoots one target component can never be used safely. Ignore those triplets and ask: can the remaining ones collectively reach every target component? One linear scan decides it.
You have a list of triplets [a, b, c] and a target = [x, y, z]. The only allowed operation is to pick two triplets and replace one of them with the component-wise max: [max(a1,a2), max(b1,b2), max(c1,c2)]. You can do this any number of times. Return true if you can reach target, otherwise false.
Concrete example. triplets = [[2,5,3],[1,8,4],[1,7,5]], target = [2,7,5].
[1,8,4]has component 2 = 8 > target[1] = 7 → skip.[2,5,3] and [1,7,5]: [max(2,1), max(5,7), max(3,5)] = [2,7,5] ✓Output: true.
true.[ta, tb, tc] = target. These are the exact values we need in each slot.[x, y, z], skip it if x > ta || y > tb || z > tc. A single over-limit component would permanently ruin that slot if merged.a = max(a, x), b = max(b, y), c = max(c, z). This simulates merging all safe triplets into one.a === ta && b === tb && c === tc. If any slot falls short, no combination of safe triplets can fill it.The greedy insight eliminates the need for any search. Because merge is monotone (values only go up), there is never a reason to use a triplet that exceeds the target — and using every safe triplet is always at least as good as using a subset.
[2,7,5]. Skip any triplet where a component exceeds the target; accumulate component-wise max over the rest.1function mergeTriplets(triplets: number[][], target: number[]): boolean {2▶ const [ta, tb, tc] = target;3▶ let a = 0, b = 0, c = 0;45 for (const [x, y, z] of triplets) {6 // Skip any triplet that could "pollute" a target component.7 if (x > ta || y > tb || z > tc) continue;89 // Safe triplet: take the component-wise max.10 a = Math.max(a, x);11 b = Math.max(b, y);12 c = Math.max(c, z);13 }1415 return a === ta && b === tb && c === tc;16}
function mergeTriplets(triplets: number[][], target: number[]): boolean {
const [ta, tb, tc] = target;
let a = 0, b = 0, c = 0;
for (const [x, y, z] of triplets) {
// Skip any triplet that could "pollute" a target component.
if (x > ta || y > tb || z > tc) continue;
// Safe triplet: take the component-wise max.
a = Math.max(a, x);
b = Math.max(b, y);
c = Math.max(c, z);
}
return a === ta && b === tb && c === tc;
}ta, tb, tc for clean comparisons. Initialize accumulators a, b, c to 0 — the identity for max.Math.max. After the loop, [a, b, c] equals what you would get by merging every safe triplet together (order irrelevant because max is commutative and associative).target[i], then take running max. Same O(n · k) time where k is the tuple length.| component-wise max / merge of tuples | greedy filter + running max |
| operation only increases values | skip if any component exceeds target |
| "can we reach target by combining?" | accumulate max over safe candidates |
| merge order doesn't matter | max is commutative — one pass suffices |
function mergeTriplets(triplets: number[][], target: number[]): boolean {
const [ta, tb, tc] = target;
let a = 0, b = 0, c = 0;
for (const [x, y, z] of triplets) {
if (x > ta || y > tb || z > tc) continue;
a = Math.max(a, x);
b = Math.max(b, y);
c = Math.max(c, z);
}
return a === ta && b === tb && c === tc;
}triplets = [[2,5,3],[1,8,4],[1,7,5]], target = [2,7,5]. Which triplet is filtered out?triplets = [[2,5,3],[1,7,5]], target = [2,7,5], the accumulators are:triplets = [[3,4,5],[4,5,6]], target = [3,2,5]. Result?