Merge Triplets to Form Target Triplet

Difficulty: Medium

You have a list of triplets [x, y, z] and one target triplet. You can merge any subset of triplets by taking the maximum at each position (merging [2, 5, 3] and [1, 7, 5] gives [2, 7, 5]).

Return whether some subset, of any size, merges into exactly the target.

Examples

Input: triplets = [[2,5,3],[1,8,4],[1,7,5]], target = [2,7,5]
Output: true

Merging [2,5,3] and [1,7,5] gives [max(2,1), max(5,7), max(3,5)] = [2,7,5], which matches the target.

Input: triplets = [[3,4,5],[4,5,6]], target = [3,2,5]
Output: false

Every triplet has a value greater than 2 in the second position, so merging can never bring that position down to 2 - merging only ever raises or keeps values, never lowers them.

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

Merging [2,5,3], [1,2,5], and [5,2,3] gives [max(2,1,5), max(5,2,2), max(3,5,3)] = [5,5,5].

Constraints

  • 1 <= triplets.length <= 10^5

  • triplets[i].length == target.length == 3

  • 1 <= triplets[i][j], target[j] <= 1000

Approach

Merging takes maximums, so it can never lower a value. A triplet that exceeds the target in any position is out: using it would push that position too high, with no way back down.

So discard every triplet that overshoots anywhere. Among the rest (every value is <= the target), check that each of the 3 positions is hit exactly by at least one triplet. If all 3 are covered, merging all the remaining triplets gives the target.

Solutions

Brute Force - Try Every Subset

Merge every subset of triplets and check whether any equals the target.

function mergeTriplets(triplets, target) {
  const n = triplets.length;
  for (let mask = 1; mask < (1 << n); mask++) {
    let merged = [0, 0, 0];
    for (let i = 0; i < n; i++) {
      if (mask & (1 << i)) {
        for (let j = 0; j < 3; j++) {
          merged[j] = Math.max(merged[j], triplets[i][j]);
        }
      }
    }
    if (merged[0] === target[0] && merged[1] === target[1] && merged[2] === target[2]) {
      return true;
    }
  }
  return false;
}

Time: O(2^n * n) - every subset of n triplets, each taking O(n) to merge · Space: O(1)

Optimal - Filter Then Check Coverage

Discard triplets that overshoot the target anywhere, since merging can't lower a value. Then check each target position is matched by at least one remaining triplet.

function mergeTriplets(triplets, target) {
  const achieved = [false, false, false];

  for (const triplet of triplets) {
    if (triplet[0] > target[0] || triplet[1] > target[1] || triplet[2] > target[2]) {
      continue;
    }
    for (let j = 0; j < 3; j++) {
      if (triplet[j] === target[j]) achieved[j] = true;
    }
  }

  return achieved[0] && achieved[1] && achieved[2];
}

Time: O(n) · Space: O(1)