32 · Greedy Algorithms

#1899 Merge Triplets to Form Target Triplet

Mediumsolve on LeetCode ↗

frequently reported atGoogle

the problem

In plain words

You get a list of triplets [a, b, c] and a target triplet [x, y, z]. One move picks two triplets i and j and replaces triplet j with their element-wise maximum: [max(aᵢ, aⱼ), max(bᵢ, bⱼ), max(cᵢ, cⱼ)]. You may make any number of moves. Return true if the target can end up as one of the triplets.

in triplets = [[2,5,3],[1,8,4],[1,7,5]], target = [2,7,5]

out true

Merge [2,5,3] and [1,7,5] → [2,7,5]. [1,8,4] would push y to 8, so leave it out.

in triplets = [[3,4,5],[4,5,6]], target = [3,2,5]

out false

Both triplets overshoot somewhere (4 > 2 and 4 > 3), and a max can never shrink a value.

in triplets = [[2,5,3],[2,3,4],[1,2,5],[5,2,3]], target = [5,5,5]

out true

Merge [2,5,3], [1,2,5] and [5,2,3] → [5,5,5].

  • 1 ≤ triplets.length ≤ 10⁵
  • triplets[i].length == target.length == 3
  • 1 ≤ every value ≤ 1000

your turn

Try it before you peek

Read the examples, say the brute force out loud, then write code: here or on LeetCode ↗. Stuck for 20 minutes? Open one hint at a time.

Solve it in the browser

LeetCode-style: complete the class. Compiled by the real javac (Java 17) and judged right here — nothing is sent anywhere.

    Solutions are hidden so you can try first. Even 20 minutes of real effort makes the answer stick far better than reading it.

    Marking the problem solved, or opening every hint, also shows it.

    practise next

    Similar problems

    1. #134
      Gas Station
      Mediumin Greedy
    2. #55
      Jump Game
      Mediumin Greedy