Algorithms · Visualized

Merge Triplets to Form Target Triplet

Greedy

Make the locally optimal choice at each step, hoping it leads to a globally optimal solution.

Given triplets of 3 numbers each, and a target triplet, determine if you can select some triplets and 'merge' them (taking the max of each position across selected triplets) to exactly form the target. Time: O(n) average, O(n), where n is the number of triplets worst; space: O(1).

Big-O: Works when the problem has optimal substructure + greedy-choice property — e.g. intervals, Huffman, fractional knapsack.

Merge Triplets to Form Target Triplet

Greedy

Given triplets of 3 numbers each, and a target triplet, determine if you can select some triplets and 'merge' them (taking the max of each position across selected triplets) to exactly form the target. The greedy filter: only consider triplets where no value exceeds the corresponding target value (an 'oversized' triplet would ruin the merge), then check if the max of each position across the valid ones hits the target exactly.

How Merge Triplets to Form Target Triplet Works

  1. Go through each triplet and discard any triplet that has any single value greater than the corresponding target value (it would push that position too high if used).
  2. For the remaining valid triplets, track the maximum value seen so far at each of the three positions.
  3. Update these three running maximums as you process each valid triplet.
  4. After processing all valid triplets, compare the three running maximums to the target triplet.
  5. If all three match exactly, it's possible to form the target; otherwise, it isn't.

Merge Triplets to Form Target Triplet Worked Example

Input: {triplets: [[2, 5, 3], [1, 8, 4], [1, 7, 5]], target: [2, 7, 5]}

Check [2,5,3]: all values ≤ target[2,7,5] → valid, running max=[2,5,3]. Check [1,8,4]: 8>7 (target position1) → discard, oversized. Check [1,7,5]: all ≤ target → valid, running max=[max(2,1),max(5,7),max(3,5)]=[2,7,5]. Final running max [2,7,5] matches target exactly — possible, return true.

Merge Triplets to Form Target TripletO(n)
Merge any triplets that never exceed the target, component-wise max.
xyz
T0253
T1184
T2175
target[2,7,5]
current[0, 0, 0]
Step 1 / 5
Default Current / Compare Swap / Write Settled Active / Path Pivot / Reject Locked (hollow) Target
Merge Triplets: merge any triplet that never exceeds the target.
Python · Greedy Algorithms in Code
def merge_triplets(triplets, target):
    ok = [False, False, False]
    for t in triplets:
        if all(t[i] <= target[i] for i in range(3)):
            for i in range(3):
                ok[i] |= t[i] == target[i]
    return all(ok)

Behind the Scenes Execution

Press ▶ Run in the visualizer — the call stack, registers and memory will replay here in sync with the code, line by line.

Merge Triplets to Form Target Triplet Time & Space Complexity

BestO(n)
AverageO(n)
WorstO(n), where n is the number of triplets
SpaceO(1)

When to Use Merge Triplets to Form Target Triplet

  • Combining resource/stat bundles (e.g. game character stats) to exactly hit a target profile without overshooting any stat.

Recommended resourcesAffiliate links · may earn us a commission

Recommended for learners

Structured coding coursesVideo courses that pair well with visual practice — pick one language and go deep.Browse courses Classic DSA interview booksThe reference books interviewers themselves learned from — great next to the visualizers.See the books Cloud hosting for side projectsShip the portfolio project that gets you hired — free credits for new accounts.Claim credits

Keep the momentum going

Step through a related structure, pattern or course — the ideas reinforce each other and stick faster.