← Whiteboard questions

Merge Triplets to Form Target Triplet

Can merging triplets by max in each place give the target?

DSA, Medium, Greedy

Which structure or algorithm, and what does it cost.

Each triplet in triplets holds three readings [a, b, c]. You may choose any group of one or more triplets and merge them into one triplet. In each of the three places, the merged triplet takes the largest reading found in that place across the group. Return True if some group merges into exactly target. Otherwise return False.

Examples

Input triplets = [[2, 6, 1], [3, 4, 4], [4, 1, 2], [3, 5, 9]], target = [3, 6, 4]
Output True
Why the triplets [2, 6, 1] and [3, 4, 4] merge into [3, 6, 4]. The other two triplets have a reading above the target.
Input triplets = [[1, 2, 3], [4, 7, 3], [1, 6, 1]], target = [4, 6, 3]
Output False
Why the only triplet with a 4 in the first place also has 7 in the second, which is above 6, and merging never lowers a reading.
Input triplets = [[5, 5, 5]], target = [5, 5, 5]
Output True
Why a single triplet is a group by itself.

Constraints

Your answer must contain

Target Aim for O(n) time and O(1) extra memory.

Hints

Try the problem first. Open one hint at a time.

Hint 1 a nudge

A merge takes the largest reading in each place. What does that do to a reading that is already too big?

Hint 2 the approach

Which triplets are usable at all? Think about what a triplet can still add to a group heading for the target.

Hint 3 the key step

Drop every triplet with a reading above the target. Then check that each of the three places has a kept triplet that hits the target value exactly.

Walkthrough

Full walkthrough brute force to best

Brute force

Try every non-empty group of triplets. Merge each group and compare it with the target. That is 2^n - 1 groups, and each merge takes O(n) time. It only works for a handful of triplets: with 40 triplets there are more than a trillion groups.

Best method

A merge never lowers a reading, so a triplet with any reading above the target can never be in the group, because the merged reading would stay too high. Drop those triplets. All the triplets that remain fit under the target in every place, so merging all of them also fits under it. The merged result equals the target in a place only if some kept triplet has the target value there. So it is enough to check that each of the three places has such a kept triplet. Dropping the over-target triplets is forced, and keeping the rest is free, since merging more triplets under the target never hurts.

Hand trace

Run on the triplets [2, 6, 1], [3, 4, 4], [4, 1, 2], [3, 5, 9] with target [3, 6, 4]. Triplet [2, 6, 1]: every reading is at most the target, so keep it. It hits the target in the second place (6). Triplet [3, 4, 4]: keep it. It hits the first place (3) and the third place (4). Triplet [4, 1, 2]: 4 is above 3, so skip it. Triplet [3, 5, 9]: 9 is above 4, so skip it. All three places have a hit, so the answer is True.

Code

def can_form_target(triplets, target):
    hit = [False, False, False]
    for t in triplets:
        if t[0] <= target[0] and t[1] <= target[1] and t[2] <= target[2]:
            for k in range(3):
                if t[k] == target[k]:
                    hit[k] = True
    return all(hit)

Time and memory

O(n) time, one pass with three comparisons per triplet. O(1) memory, three flags.

Common mistakes

  • Merging every triplet. One triplet that goes above the target in any place ruins the group.
  • Counting a triplet that hits the target in one place without checking its other two readings. A triplet counts only when all three readings are at most the target.
  • Asking for one single triplet to equal the target. The hits can come from different triplets, one for each place.

Related problems

More Greedy problems
Merge Triplets to Form Target Triplet walkthrough