Greedy The best choice now
496. Merge Triplets to Form Target Triplet
A triplet is a list of three integers. You are given a list of triplets, triplets, and one more
triplet, target.
To merge two triplets, we take the larger value in each position. So merging [a, b, c] with
[x, y, z] gives [max(a, x), max(b, y), max(c, z)]. You may merge any two triplets you have, including
the results of earlier merges, as many times as you like. You do not have to use every triplet, and a
triplet from the list counts as made even without a merge.
Return true if you can make a triplet equal to target, and false otherwise.
Example 1
Input: triplets = [[4, 1, 6],
[2, 7, 3],
[5, 2, 9],
[1, 1, 6]]
target = [4, 7, 6]
trueExplanation: Merging [4,1,6] with [2,7,3] gives [4,7,6]. We leave [5,2,9] out: its 5 and 9 are above the
target, and a merge never makes a value smaller.
Example 2
Input: triplets = [[3, 8, 2], [6, 1, 2], [7, 2, 5]]
target = [6, 8, 5]
falseExplanation: Only [7,2,5] has a 5 in the last position, but its 7 is above the target's 6. Without it, the last
position never gets past 2.
Example 3
Input: triplets = [[1000000000, 5, 1],
[1, 5, 1000000000]]
target = [1000000000, 5, 1000000000]
trueExplanation: Merging the two triplets gives the target. Both have a 5 in the middle, so the middle stays 5.
Check your understanding
Work out the answer for this input before you write any code.
Constraints
1 ≤ triplets.length ≤ 105triplets[i].length = target.length = 31 ≤ triplets[i][j], target[j] ≤ 109
Clarifying questions
What you would ask an interviewer before writing any code.