Skip to content
BASA DSA

Merge Triplets to Form Target Triplet

MediumGreedy

Choose any nonempty subset of triplets and take their coordinate-wise maximum. Return whether some such maximum equals target exactly.

Example 1

Input
triplets = [[2,1,3],[1,4,2],[2,4,1]]
target = [2,4,3]
Output
true

Explanation

Taking maxima of [2,1,3] and [1,4,2] gives [2,4,3].

Example 2

Input
triplets = [[3,1,1],[1,3,1]]
target = [2,2,1]
Output
false

Explanation

Each triplet exceeds a target coordinate, and maxima cannot lower it.

Example 3

Input
triplets = [[1,2,3]]
target = [1,2,3]
Output
true

Explanation

The sole triplet already equals the target.

Constraints

  • 1 <= number of triplets <= 100000
  • Every triplet and target has three positive coordinates <= 1000

Function contractmergeTripletsArguments: json, json
Loading draft…
SolutionRuns on your device
Loading draft…