Merge Triplets to Form Target Triplet

Medium

Topics
ArrayGreedy

You are given triplets and a target triplet. A merge replaces the running triplet with the element-wise maximum of it and a chosen triplet. Return true if some sequence of merges can produce exactly target.

Example 1

025311842175012
2D grid — one row per line.
Input:  triplets = [[2,5,3],[1,8,4],[1,7,5]], target = [2,7,5]
Output: true

Example 2

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

Constraints

  • 1 <= triplets.length <= 10^5
  • triplets[i].length == target.length == 3
Editorial — intuition, approach, complexity and pitfalls

Intuition

A triplet is only usable if none of its coordinates exceeds the target (otherwise a merge would overshoot). Among usable triplets, you just need each target coordinate to be hit exactly by at least one of them.

Approach

  1. Ignore any triplet with a coordinate greater than the corresponding target value.
  2. For each usable triplet, mark which coordinates equal the target.
  3. Return true iff all three coordinates get marked.

Dry Run

triplets [[2,5,3], [1,8,4], [1,7,5]]   target [2,7,5]
Merging takes the coordinate-wise maximum, which is monotone: once a coordinate
rises it never falls. So a triplet exceeding the target ANYWHERE can never be
used, and every usable triplet contributes its exact matches for free.
  [2,5,3]   within target in all three coordinates   matches target[0]   good {0}
  [1,8,4]   8 > 7, so DISCARDED entirely
  [1,7,5]   within target                            matches target[1] and
                                                      target[2]         good {0,1,2}
all three coordinates covered  ->  true
No merge is ever simulated. That is the insight: because the maximum is taken
independently per coordinate, "can some subset merge to exactly the target" reduces
to "is each coordinate achieved by at least one safe triplet".
The filter is the whole algorithm, not a guard. Drop it and a triplet that
overshoots still gets to donate its matching coordinates: on [[1,3,4], [2,5,8]]
with target [2,5,4] the answer is false -- the second triplet's 8 would push
coordinate 2 past 4 -- but without the filter it donates its matching 2 and 5 and
the result comes back true.

Complexity

  • Time — O(n): a single pass over triplets.
  • Space — O(1): a tiny set of at most 3 indices.

Common Pitfalls

  • Only triplets that never exceed the target in any coordinate are safe to merge, and that filter is the algorithm rather than a guard. Drop it and an overshooting triplet still donates its matching coordinates: on [[1,3,4],[2,5,8]] with target [2,5,4] the answer is false, because the 8 would push the third coordinate past 4, but without the filter it contributes its 2 and 5 and the result comes back true.

Full Solution — all languages

class Solution:
    def mergeTriplets(self, triplets, target):
        good=set()
        for t in triplets:
            if t[0]<=target[0] and t[1]<=target[1] and t[2]<=target[2]:
                for i in range(3):
                    if t[i]==target[i]: good.add(i)
        return len(good)==3
SensAI
Beta
Tap the mic to stop voice mode