354. Merge Triplets to Form Target Triplet

MediumGreedy

A triplet is an array of three integers. You are given a 2D integer array triplets, where triplets[i] = [a_i, b_i, c_i] describes the i-th triplet. You are also given an integer array target = [x, y, z] that describes the triplet you want to obtain. To obtain target, you may apply the following operation on triplets any number of times: choose two indices i and j and update triplets[j] to [max(a_i, a_j), max(b_i, b_j), max(c_i, c_j)]. Return true if it is possible to obtain the target triplet [x, y, z] as an element of triplets, or false otherwise.

Examples

Input: [[2,5,3],[1,8,4],[1,7,5]] [2,7,5]

Output: true

Explanation: Using only triplets that never exceed the target, positions 0,1,2 each hit the target value.

Constraints

  • 1 <= triplets.length <= 10^5
  • triplets[i].length == target.length == 3
  • 1 <= a_i, b_i, c_i, x, y, z <= 1000
Loading...

Run checks all cases above. Submit evaluates all test cases.