Merge Triplets to Form Target Triplet
Medium · Greedy · Explanation and code example
Greedy (Optimal)
How it works
We are given several triplets and a target triplet. When we merge triplets, we take the maximum value at each index, so values can only increase, never decrease.
This leads to an important rule:
- Any triplet that has a value greater than the target at any index cannot be used to form the target.
Instead of collecting indices in a set, we can think more directly:
- To reach
target[0], we need at least one triplet where:- the first value equals
target[0] - the other two values do not exceed the target
- the first value equals
- Similarly for
target[1]andtarget[2]
If we can independently satisfy all three positions using valid triplets, then merging those triplets will exactly form the target.
Steps
- Initialize three boolean flags:
x→ can we matchtarget[0]?y→ can we matchtarget[1]?z→ can we matchtarget[2]?
- Iterate through each triplet
t: - Update the flags:
- Set
x = trueif:t[0] == target[0]t[1] <= target[1]t[2] <= target[2]
- Set
y = trueif:t[1] == target[1]t[0] <= target[0]t[2] <= target[2]
- Set
z = trueif:t[2] == target[2]t[0] <= target[0]t[1] <= target[1]
- Set
- If at any point all three flags
x,y, andzbecometrue:- return
trueimmediately
- return
- If the loop finishes and not all flags are
true:- return
false
- return
Complexity
- Time complexity:
O(n) - Space complexity:
O(1)
Things to watch for
Including Triplets With Values Exceeding the Target
A triplet where any element exceeds the corresponding target value can never be used because merging only takes the maximum at each position, so values can only increase. Failing to filter out these invalid triplets leads to incorrect results.
Checking Only for Exact Target Matches Without Filtering
Some solutions check if a triplet matches the target at a position but forget to verify that the other positions do not exceed the target. A triplet like [5, 2, 3] matching target[0] = 5 is useless if target[1] = 1 because the 2 exceeds it.
Thinking You Need to Find a Single Triplet Equaling the Target
The problem allows merging multiple triplets. You do not need one triplet that exactly equals the target. You need to find triplets that collectively can contribute each target value independently while never exceeding any target position.
Python code example
from typing import List
class Solution:
def mergeTriplets(self, triplets: List[List[int]], target: List[int]) -> bool:
x = y = z = False
for t in triplets:
x |= (t[0] == target[0] and t[1] <= target[1] and t[2] <= target[2])
y |= (t[0] <= target[0] and t[1] == target[1] and t[2] <= target[2])
z |= (t[0] <= target[0] and t[1] <= target[1] and t[2] == target[2])
if x and y and z:
return True
return False
C++ code example
class Solution {
public:
bool mergeTriplets(vector<vector<int>>& triplets, vector<int>& target) {
bool x = false, y = false, z = false;
for (const auto& t : triplets) {
x |= (t[0] == target[0] && t[1] <= target[1] && t[2] <= target[2]);
y |= (t[0] <= target[0] && t[1] == target[1] && t[2] <= target[2]);
z |= (t[0] <= target[0] && t[1] <= target[1] && t[2] == target[2]);
if (x && y && z) return true;
}
return false;
}
};
Source and license
Adapted from NeetCode’s source · MIT license. Node types are supplied by LeetCode where needed.