oneby7

Greedy The best choice now

496. Merge Triplets to Form Target Triplet

Medium

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]

Output: true

Explanation: 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]

Output: false

Explanation: 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]

Output: true

Explanation: 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.

triplets = [[5, 1, 2], [1, 4, 2], [5, 4, 9]] target = [5, 4, 2]

Constraints

  • 1 ≤ triplets.length ≤ 105
  • triplets[i].length = target.length = 3
  • 1 ≤ triplets[i][j], target[j] ≤ 109

Clarifying questions

What you would ask an interviewer before writing any code.

Hints

Interview follow-ups

Runs in your browser
Code
Runs in your browser for now · some things may not work as expectedLn 1, Col 1

Article

Send feedback

Thank you for helping. We read every report and fix what is wrong.

How do you feel about oneby7?

Sign in to oneby7

Keep your progress, your streak and your code, on any device.

New here? This creates your account too.