Merge Triplets to Form Target
A medium Greedy problem included in Love Babbar 450. Below: the roles whose interviews prioritise this topic, and how to practise it.
- Topic
- Greedy
- Sheets
- 1
- Core for
- 3 roles
- Platform
- LeetCode
The problem
Given a 2D array of triplets and a target triplet, you may choose any subset of triplets and for each position take the maximum value. Return true if the resulting triplet can equal the target.
Example 1
- Input
- triplets = [[2,5,3],[1,8,4],[1,7,5]], target = [2,7,5]
- Output
- true
- Why
- Choosing triplets [2,5,3] and [1,7,5]: max values are [2,7,5] which equals target.
Example 2
- Input
- triplets = [[3,4,5],[4,5,6]], target = [3,2,5]
- Output
- false
- Why
- No subset of triplets can produce a second value of 2 or less.
Constraints
- 1 <= triplets.length <= 10^5
- triplets[i].length == 3
- 1 <= triplets[i][j], target[j] <= 1000
How to think about it
Updated 2026-09-09Any triplet that exceeds target in even one position can never be picked, because taking a component-wise maximum can never decrease a value. Conversely, any triplet that does not exceed target in any position is completely harmless to include, so every such triplet can be taken greedily.
Approaches, worst first
Subset powerset exploration
time O(2^n) · space O(n)
Generate combinations of triplets and compute their element-wise max. Explores an exponential search space when an individual element check tells us immediately whether a triplet is poison.
Greedy filter and component matchWrite this one
time O(n) · space O(1)
Scan every triplet once. If t[0] <= target[0] and t[1] <= target[1] and t[2] <= target[2], record matches for any component where t[j] == target[j]. If all three components find at least one valid contributor, return true.
Where people lose marks · 2
- Accepting a triplet because it matches target on one index while ignoring that it strictly exceeds target on another index, which corrupts the maximum.
- Requiring a single triplet to match target on multiple indices simultaneously; different triplets can contribute the target values for each of the three positions.
The theory behind it
Greedy — the ground this problem stands on. All Greedy problems
What Greedy is
A greedy algorithm makes the best-looking choice available right now, at every step, without ever looking back or second-guessing its decision. Think of a cashier making change by handing over the largest possible coin first, repeatedly, until the total is reached. Unlike dynamic programming, which saves and compares answers to multiple overlapping paths, a greedy strategy commits to one immediate option and keeps moving forward.
When to reach for it
Reach for greedy when problems ask for minimum jumps, interval scheduling, assigning resources to maximize satisfaction, or finding fractional values. Key signals include sorted orders where greedily taking the next item never hurts future options, or gas station round trips where running balances prove reachability. If you can prove that taking the immediate best choice never leaves you worse off than any alternative, greedy gives the fastest answer.
How the pattern works
Start by sorting the input to bring the most promising candidates to the front. At each position, evaluate your local rule, take the best available piece, and update your running state. The crucial mental step is proving the greedy choice property: demonstrate that picking this immediate winner cannot block a better global solution down the road. If choosing an item now forces you to reconsider past decisions when conditions change later, greedy fails and you must switch to dynamic programming instead.
What each operation costs
| Operation | Time |
|---|---|
| sort elements to enable greedy selection | O(n log n) |
| greedy single-pass scan through sorted input | O(n) |
| greedy choice using a priority queue | O(n log n) |
What usually goes wrong with Greedy
- Applying a greedy choice without proving it yields the global optimum, such as picking the largest coin first for arbitrary denominations where dynamic programming was required.
- Forgetting to sort the input before running the greedy loop, making local decisions on unordered elements that produce invalid answers.
- Picking items based on only one attribute when the optimal decision depends on a ratio or combination of multiple attributes.
Which roles need this problem
Greedy is a core topic for these 3 roles — if you're targeting one of them, this problem is early in your path, not optional.
Secondary for 4 more roles, including Site Reliability Engineer, Search Engineer, Quant Developer.
Track this in your role's order
Pick your target role and all 370 problems — including this one — resequence to what that interview actually asks. Free.
Start freeMore Greedy problems
Problem set and role mapping as of .