You are given a 2D array of integers triplets, where triplets[i] = [ai, bi, ci] represents the ith triplet. You are also given an array of integers target = [x, y, z] which is the triplet we want to obtain.
To obtain target, you may apply the following operation on triplets zero or more times:
Choose two different triplets triplets[i] and triplets[j] and update triplets[j] to become [max(ai, aj), max(bi, bj), max(ci, cj)].
* E.g. if triplets[i] = [1, 3, 1] and triplets[j] = [2, 1, 2], triplets[j] will be updated to [max(1, 2), max(3, 1), max(1, 2)] = [2, 3, 2].
Return true if it is possible to obtain target as an element of triplets, or false otherwise.
Example 1:
Input: triplets = [[1,2,3],[7,1,1]], target = [7,2,3]
Output: trueExplanation:
Choose the first and second triplets, update the second triplet to be [max(1, 7), max(2, 1), max(3, 1)] = [7, 2, 3].
Example 2:
Input: triplets = [[2,5,6],[1,4,4],[5,7,5]], target = [5,4,6]
Output: falseConstraints:
1 <= triplets.length <= 10001 <= ai, bi, ci, x, y, z <= 100
You should aim for a solution with O(n) time and O(1) space, where n is the size of the input array.
An important observation is that we can ignore triplets with values greater than the target triplet.
Specifically, if a triplet t has any element greater than the corresponding value in target (i.e., t[0] > target[0], t[1] > target[1], or t[2] > target[2]), we can discard it. This is because using such a triplet in operations would exceed the target values, making it invalid.
Now, from the remaining valid triplets, we only need to check whether the target triplet values exist. Since all values in the valid triplets are less than or equal to the corresponding values in the target triplet, finding the target triplet among them guarantees that we can achieve it.
Before attempting this problem, you should be comfortable with:
We are given several triplets and a target triplet.
We can merge triplets by taking the maximum value at each index, and we want to know if it is possible to obtain the target exactly.
A key observation is:
For the remaining valid triplets:
If we can find triplets that collectively cover all three indices of the target, then merging them will produce the target.
good to track which target indices can be matched.t:t is greater than the corresponding value in target, skip this tripletit[i] == target[i], add index i to the set good{0, 1, 2} are present in good, return truefalseclass Solution:
def mergeTriplets(self, triplets: List[List[int]], target: List[int]) -> bool:
good = set()
for t in triplets:
if t[0] > target[0] or t[1] > target[1] or t[2] > target[2]:
continue
for i, v in enumerate(t):
if v == target[i]:
good.add(i)
return len(good) == 3We 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:
Instead of collecting indices in a set, we can think more directly:
target[0], we need at least one triplet where:target[0]target[1] and target[2]If we can independently satisfy all three positions using valid triplets, then merging those triplets will exactly form the target.
x → can we match target[0]?y → can we match target[1]?z → can we match target[2]?t:x = true if:t[0] == target[0]t[1] <= target[1]t[2] <= target[2]y = true if:t[1] == target[1]t[0] <= target[0]t[2] <= target[2]z = true if:t[2] == target[2]t[0] <= target[0]t[1] <= target[1]x, y, and z become true:true immediatelytrue:falseclass 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 FalseA 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.
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.
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.