SDE & MLE Prep

1899. Merge Triplets to Form Target Triplet

Asked at 1Google20

Problem

You may repeatedly pick two triplets and replace the second with their element-wise maximum. Decide whether target can end up as one of the triplets.

Example: triplets = [[2,5,3],[1,8,4],[1,7,5]], target = [2,7,5] → true

Constraints: up to 10⁵ triplets.

Ignore any triplet with a value exceeding the target in some coordinate (it would poison the max). Among the rest, check that each target coordinate is matched by at least one.

Complexity: O(n).

class Solution:
    def mergeTriplets(self, triplets: List[List[int]], target: List[int]) -> bool:
        good = set()
        for t in triplets:
            if all(t[i] <= target[i] for i in range(3)):
                good |= {i for i in range(3) if t[i] == target[i]}
        return len(good) == 3