BackendML

Merge Triplets to Form Target Triplet

Medium3 min read
The Merge Triplets to Form Target Triplet problem requires determining whether a target triplet can be formed by combining elements from the given triplets.

Problem

You are given a 2D integer array triplets, where triplets[i] = [a, b, c] represents a triplet, and an integer array target = [x, y, z] representing the target triplet.

You can choose any number of triplets and merge them by taking the maximum value at each corresponding position. The merged triplet is:
[max(a values), max(b values), max(c values)]
Return true if it is possible to form the target triplet. Otherwise, return false.

Example 1

Input
triplets = [[2,5,3],[1,8,4],[1,7,5]]
target = [2,7,5]
Output
true
Explanation
Choose the first and third triplets. Their merged result is:
[max(2,1), max(5,7), max(3,5)] = [2,7,5]
This matches the target triplet.

Example 2

Input
triplets = [[3,4,5],[4,5,6]]
target = [3,2,5]
Output
false
Explanation
The second element of every available triplet is greater than the target's second element, which is 2. Since merging takes the maximum value at each position, the second element cannot be reduced to 2.

Example 3

Input
triplets = [[2,3,4],[1,2,5],[2,2,3]]
target = [2,3,5]
Output
true
Explanation
The first triplet provides the first and second target values, while the second triplet provides the third target value. Merging them produces [2,3,5].

Solution

This problem can be solved using a Greedy Approach. The key observation is that merging triplets takes the maximum value at each position. Therefore, a value greater than the corresponding target value can never be reduced to the target value.

For a triplet [a, b, c] to be useful, all three of its elements must be less than or equal to the corresponding target elements:
a <= x
b <= y
c <= z
If any element exceeds its corresponding target value, we can safely ignore that triplet because including it would make the merged result exceed the target at that position.

For every remaining valid triplet, we check which target values it can contribute:

- If a == x, we have found a triplet that provides the target's first element.
- If b == y, we have found a triplet that provides the target's second element.
- If c == z, we have found a triplet that provides the target's third element.

We maintain three boolean variables to track whether each target element can be obtained. After processing all triplets, we return true only if all three variables are true.

The target elements do not need to come from the same triplet. Different valid triplets can provide different target elements, and merging them will form the target as long as no element exceeds its corresponding target value.
class Solution {
    public boolean mergeTriplets(int[][] triplets, int[] target) {
        boolean first = false;
        boolean second = false;
        boolean third = false;

        for (int[] triplet : triplets) {
            if (triplet[0] > target[0] ||
                    triplet[1] > target[1] ||
                    triplet[2] > target[2]) {
                continue;
            }

            if (triplet[0] == target[0]) {
                first = true;
            }

            if (triplet[1] == target[1]) {
                second = true;
            }

            if (triplet[2] == target[2]) {
                third = true;
            }

            if (first && second && third) {
                return true;
            }
        }

        return first && second && third;
    }
}

Complexity

Let n be the number of triplets. We iterate through each triplet once and perform a constant number of comparisons and updates. Therefore, the time complexity is O(n).

We use only three boolean variables to track the target elements. The algorithm requires O(1) auxiliary space, excluding the input arrays.

Comments

No comments yet. Ask a question or share what helped.