Skip to content
AI360Xpert
Beta
Difficulty: MediumGreedy

Merge Triplets to Form Target Triplet

Problem in Plain English

You are given a list of integer triplets and a target triplet, and you may repeatedly replace any triplet with the element-wise maximum of two triplets. Return whether any sequence of such merges can produce the target triplet.

Problem Statement

You are given a 2D integer array triplets where triplets[i] = [a, b, c] is the ith triplet, and an integer array target = [x, y, z] describing the triplet you want to obtain.

In one operation you may pick two indices i and j with i != j and replace triplets[j] with [max(a_i, a_j), max(b_i, b_j), max(c_i, c_j)]. For example, merging [2, 5, 3] into [1, 7, 5] gives [max(2, 1), max(5, 7), max(3, 5)] = [2, 7, 5]. Return true if the target triplet can ever appear in triplets, otherwise return false.

Constraints

  • 1 <= triplets.length <= 10^5
  • triplets[i].length == target.length == 3
  • 1 <= a, b, c, x, y, z <= 1000

Examples

Example 1

Input
triplets = [[2,5,3],[1,8,4],[1,7,5]], target = [2,7,5]
Output
true

Merge [2,5,3] into [1,7,5] to get [max(2,1), max(5,7), max(3,5)] = [2,7,5], which equals the target.

Example 2

Input
triplets = [[3,4,5],[4,5,6]], target = [3,2,5]
Output
false

No triplet holds 2 in the middle position, so the target can never be formed.

Example 3

Input
triplets = [[2,5,3],[2,3,4],[1,2,5],[5,2,3]], target = [5,5,5]
Output
true

Merge [2,5,3] into [1,2,5] to get [2,5,5], then merge [2,5,5] into [5,2,3] to get [5,5,5].

Example 4

Input
triplets = [[2,7,5]], target = [2,7,5]
Output
true

The only row is already the target, so zero operations suffice. Two distinct indices are needed only when actually performing a merge.

Example 5

Input
triplets = [[2,8,5],[1,7,4]], target = [2,7,5]
Output
false

The first row supplies 2 and 5 but also overshoots the middle coordinate. The only safe row [1,7,4] cannot supply either missing value, so checking coordinates without filtering would give a false positive.

Intuition

A maximum can raise a coordinate but can never lower it. For target [2,7,5], row [1,8,4] is unusable because its middle 8 would survive every later merge. Safe rows [2,5,3] and [1,7,5] can be combined: one supplies the first coordinate 2, the other supplies 7 and 5. The useful question is whether safe rows collectively supply all three target values.

Approaches

Subset Enumeration

Solution Details

Reveal Subset Enumeration: intuition, complexity, and code

Hints

Hint 1
Merging takes element-wise maxima, so no coordinate can ever decrease once it grows past its target value.
Hint 2
Any triplet with a coordinate strictly larger than the matching target coordinate can never be used, so discard it outright.
Hint 3
After discarding those rows, check the three coordinates independently: some kept triplet must equal the target in each position.

Edge Cases

  • A triplet already equal to the target returns true without any merge taking place.
  • A coordinate value missing from every triplet, like the middle 2 in Example 2, makes the answer false no matter what is merged.
  • Duplicate triplets change nothing, since the element-wise maximum of identical rows is the same row.
  • If every row is unsafe, no witness flag can be set; the answer is false. A safe row with every coordinate strictly below the target also contributes no exact match.

Common Mistakes and Interview Tips

  • Merging every triplet including ones that overshoot the target, then watching the running maximum exceed target and calling it impossible.
  • Demanding one single triplet that matches all three coordinates instead of collecting the three matches independently across safe rows.
  • Setting flags from overshooting rows before filtering them, which reports true for targets that can never actually form.
  • The initial [0,0,0] is a search identity, not an actual row; its use relies on the strictly positive constraint.
  • Interview tip: explain why witnesses must come from safe rows, and why at most three distinct witness rows suffice. The recursion is only a baseline and cannot support maximum bounds.

Key Takeaway

When an operation only moves values in one direction, inputs that overshoot the goal are permanently useless: filter them out and the greedy choice becomes checking each coordinate independently.