DSAPrep
MediumGreedy

Merge Triplets to Form Target Triplet

A triplet is an array of three integers. You are given a 2D integer array triplets, where triplets[i] = [a_i, b_i, c_i] describes the ith triplet. You are also given an integer array target = [x, y, z] that describes the triplet you want to obtain.

To obtain target, you may apply the following operation on triplets any number of times (possibly zero): choose two indices i and j (i != j) and update triplets[j] to become [max(a_i, a_j), max(b_i, b_j), max(c_i, c_j)].

Return true if it is possible to obtain the target triplet as an element of triplets, or false otherwise.

Example 1

Input: triplets = [[2,5,3],[1,8,4],[1,7,5]], target = [2,7,5]
Output: true
Explanation: Merge the first and last triplets: [max(2,1), max(5,7), max(3,5)] = [2,7,5], which equals target.

Example 2

Input: triplets = [[3,4,5],[4,5,6]], target = [3,2,5]
Output: false
Explanation: No triplet has a 2 in the middle position, and merging can only ever increase values, so a 2 is unreachable there.

Example 3

Input: triplets = [[2,5,3],[2,3,4],[1,2,5],[5,2,3]], target = [5,5,5]
Output: true
Explanation: Merge triplet 0 with triplet 2 to get [2,5,5], then merge that with triplet 3 to get [5,5,5].

Constraints

  • 1 <= triplets.length <= 10^5
  • triplets[i].length == target.length == 3
  • 1 <= a_i, b_i, c_i, x, y, z <= 1000
View original on LeetCode ↗

Naive: Max Over Everything

Time O(n)Space O(1)

A first instinct is: take the element-wise maximum across all triplets and check whether it equals target.

class Solution:
def mergeTriplets(self, triplets: list[list[int]], target: list[int]) -> bool:
best = [0, 0, 0]
for t in triplets:
best[0] = max(best[0], t[0])
best[1] = max(best[1], t[1])
best[2] = max(best[2], t[2])
return best == target

This is wrong. Merging is a real choice — you never have to merge in a triplet that would push a coordinate past what you need. But this code force-includes every triplet’s every coordinate, including ones that overshoot target. For example, with triplets = [[2,5,3],[3,999,4],[1,7,5]] and target = [2,7,5], the answer should be true (ignore the middle triplet, merge the first and last), but this naive max gives [3,999,5], which is not target — a false negative. Runs in O(n) time but produces the wrong answer.

Greedy: Filter, Then Max

OptimalTime O(n)Space O(1)

The fix is one filtering step: a triplet is only ever safe to merge in if none of its coordinates exceeds the corresponding coordinate of target — since merges only take maximums, any coordinate already too large can never be brought back down, so including such a triplet can only hurt, never help. Discard every triplet with any coordinate > target, then take the element-wise max of what remains and compare to target.

class Solution:
def mergeTriplets(self, triplets: list[list[int]], target: list[int]) -> bool:
best = [0, 0, 0]
for t in triplets:
if t[0] <= target[0] and t[1] <= target[1] and t[2] <= target[2]:
best[0] = max(best[0], t[0])
best[1] = max(best[1], t[1])
best[2] = max(best[2], t[2])
return best == target

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

  • [2,5,3]: 2<=2, 5<=7, 3<=5 — safe. best = [2,5,3].
  • [1,8,4]: 8 > 7 — discard (it can never help; using it would inject an 8 into the middle coordinate that could never be reduced back to 7).
  • [1,7,5]: 1<=2, 7<=7, 5<=5 — safe. best = [max(2,1), max(5,7), max(3,5)] = [2,7,5].

best == targettrue, matching the expected merge of triplets 0 and 2.

Why it’s correct: merging is monotone — every coordinate can only grow, never shrink. So a triplet with any coordinate exceeding target is permanently disqualified from ever contributing to reaching target exactly; it isn’t just unhelpful, it is actively poisonous if used. Among the triplets that are safe (no coordinate exceeds target), taking the coordinate-wise max of all of them is exactly what repeated merging can achieve, and it is the best possible result — if even that maximum falls short of target in some coordinate, no sequence of merges can reach it, because no available triplet supplies that value there. Complexity: one pass, three running values → O(n) time, O(1) space.