Merge Triplets to Form Target Triplet

MediumGreedy

Problem

Given a list of triplets and a target triplet, determine whether you can choose a subset of the triplets and merge them — taking the elementwise maximum across all chosen triplets — to form exactly the target triplet. A triplet is only safe to use if none of its values exceed the corresponding target value.

Examples

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

Constraints

  • 1 <= triplets.length <= 10^5

Solve it in the editor. Sign in free to run your Python or JavaScript against test cases, get a verdict, and track your attempts.

Solve on FeatCode →

How to approach it: the Greedy pattern

Make the choice that looks best right now, without reconsidering it later, and trust that a sequence of locally optimal choices adds up to a globally optimal answer. It only works when the problem actually has that property.

Look for this pattern when

  • Sorting the input by some key, then making one pass with simple local decisions, intuitively seems to produce the best answer.
  • The problem involves intervals, scheduling, or resource allocation where "always take the best available option" tends to work.

Read the full Greedy guide →

Video walkthroughs

Original problem on LeetCode ↗

More Greedy problems