Browse curriculum

Merge Triplets to Form Target Triplet

Decide whether merging triplets by element-wise max can produce a target: drop every triplet that overshoots it, merge all the rest.

Problem Understanding

Merge Triplets to Form Target Triplet: you can merge two triplets by taking the maximum in each position, and you may repeat this with any triplets you choose. Return whether you can end up with exactly target.

Examples: triplets = [[2,5,3],[1,8,4],[1,7,5]], target = [2,7,5] returns true; triplets = [[3,4,5],[4,5,6]], target = [3,2,5] returns false.

The Intuition: Values Only Go Up

A merge can only raise values, never lower them. That settles everything:

  1. A triplet with any value above the target is poison. Merge it and that position overshoots for good, so skip it.
  2. Every other triplet is harmless. It can raise a position to the target at most, so merging it never hurts.
  3. So merge all the harmless ones and check whether every position reached the target.

This is a greedy argument: taking every safe option is never worse than taking some of them. The same "filter out what can never work, then take everything else" shape appears in Partition Labels and Jump Game II, where the greedy choice is also justified by never closing off a better answer.

Interactive Walkthrough

RED = ABOVE THE TARGET

best = [0, 0, 0]

target

2
7
5

#0

2
5
3

#1

1
8
4

#2

1
7
5

best

0
0
0

merged

0

best

0,0,0

result

:

 

Target [2, 7, 5]. best = [0, 0, 0]: nothing merged yet

Next

[2, 5, 3]: every value ≤ the target, so it is safe to merge. best = max([0, 0, 0], [2, 5, 3]) = [2, 5, 3]

Free preview: the first 3 of 5 steps

triplets

target

The target is on top and best at the bottom. Each row is a triplet: merged ones are tinted, and a skipped one is struck out with the value that broke it in red. Try One short: every triplet is safe, yet the last position never reaches 2.

Dry Run Table

Input: triplets = [[2,5,3],[1,8,4],[1,7,5]], target = [2,7,5].

Triplet All ≤ target? Action best
[2,5,3] yes merge [2,5,3]
[1,8,4] no, 8 > 7 skip [2,5,3]
[1,7,5] yes merge [2,7,5]

Answer: best = [2,7,5] equals the target, so true.

The Solution Template

One pass: filter by "no value above the target", merge with element-wise max, compare at the end.

Merge Triplets to Form Target Triplet Code
1var mergeTriplets = function(triplets, target) {
2 const best = [0, 0, 0];
3 for (const t of triplets) {
4 if (t[0] <= target[0] && t[1] <= target[1] && t[2] <= target[2]) {
5 for (let i = 0; i < 3; i++) best[i] = Math.max(best[i], t[i]);
6 }
7 }
8 return best[0] === target[0] && best[1] === target[1] && best[2] === target[2];
9};

Edge Cases & Common Mistakes

  • Checking only one position: a triplet is unusable if any of its three values is above the target.
  • Requiring an exact match per triplet: no single triplet has to equal the target; different triplets can supply different positions.
  • Comparing arrays by reference: in JavaScript, best === target compares references; compare the three values.
  • Early exit: you may return true as soon as best equals the target; the answer is the same.
  • No safe triplets: best stays [0, 0, 0], which never equals a target of positive values, so the answer is false.

Summary

Skip any triplet with a value above the target, merge every other triplet into best with element-wise max, and return whether best equals the target. O(n) time, O(1) space.

More practice: DSA interview questions with answers · patterns cheat sheet

The approach, step by step

  1. Start from zero

    best = [0, 0, 0].

  2. Filter and merge

    For each triplet, if every value is at most the target's value in the same position, set best to the element-wise max of best and the triplet; otherwise skip it.

  3. Compare

    Return whether best equals the target.

Frequently asked questions

Why is it safe to merge every triplet that does not overshoot?

Merging takes the maximum in each position, so values only go up. A triplet whose values are all at or below the target can raise a position to at most the target value, never past it, so including it can never spoil an answer. Merging all of them gives the largest result you can reach without overshooting.


Why must a triplet with one value above the target be skipped?

Once a value above the target is merged in, that position stays above the target forever, because later merges only take maxima. So such a triplet can never be part of a valid merge, whatever its other two values are.


Do I need to try different subsets or orders?

No. The max operation does not care about order, and adding a safe triplet never hurts. So the answer is simply whether the element-wise max of all safe triplets equals the target. That is one pass, O(n) time and O(1) extra space.

New to the technique behind this problem? Watch it run step by step:

Greedy Algorithms Visualization

Applying for software engineering roles? Free, no sign-up:

AI resume checker: match your resume to the job description