↻one more pass.
Greedy · Medium

Merge Triplets to Form Target Triplet: the idea to remember.

Decide whether coordinatewise maximum merges of available triplets can produce one target triplet.

Solve on LeetCode ↗

The core insight

Ignore any triplet exceeding a target coordinate, then track which exact target coordinates eligible triplets supply.

Why it works

Taking coordinatewise maxima never decreases a value; eligible triplets merge safely and must jointly supply every target coordinate.

The mistake to avoid

A triplet matching one coordinate is unusable if it overshoots another.

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

Commented Java solution

Use java.util.* imports and a surrounding solution class. Tree snippets assume the standard TreeNode definition; linked lists use ListNode and Clone Graph uses a Node with val and neighbors; Copy Random List uses Node with val, next and random. Read the input assumptions above.

// Java reference adapted from neetcode-gh/leetcode (MIT).
// Ignore any triplet exceeding a target coordinate, then track which exact target coordinates eligible triplets supply.
public boolean mergeTriplets(int[][] triplets, int[] target) {
    boolean[] greedy = new boolean[3];
    loop:for (int[] triplet : triplets) {
        for (int i = 0; i < 3; i++) if (
            triplet[i] > target[i]
        ) continue loop;

        for (int i = 0; i < 3; i++) if (triplet[i] == target[i]) greedy[i] =
            true;
    }

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

Java reference adapted from neetcode-gh/leetcode · MIT license

Make the next connection

Independent revision notes with credited Java references. Not affiliated with LeetCode or the source sheets.