Shubham Sourabhthe room · director's cutplain résumé
Room 04 · The Grind — Greedy

Merge Triplets to Form Target Triplet

MEDIUM✓ CHIP-TIMEDLC #1899 — FULL STATEMENT

The briefingthe full drill, in my own words

The drill: A pile of coordinate triples can be merged by taking the elementwise max of whichever ones you pick. Decide whether some selection of them merges into an exact target triple.

A collection of coordinate triples arrives alongside one target triple. Any subset of the collection can be merged together by taking, coordinate by coordinate, the maximum value across everything picked.

Merging can only push a coordinate up toward the largest value present among the picks — it never lowers anything. The task is deciding whether some subset of the triples merges into a result matching the target exactly on all three coordinates.

A triplet may be left out of the selection entirely, and the order triples are merged in doesn't affect the outcome — only which ones get included matters.

  • each triplet and the target hold exactly three coordinate values
  • coordinate values are positive integers within a modest range
  • merging takes the elementwise maximum, which never decreases a coordinate
  • any subset of the triplets — including skipping some — may be merged
EX 01
triplets = [[4, 3, 2], [1, 8, 3], [2, 5, 6], [9, 1, 1]] · target = [4, 8, 6]
→ true
three contributors plus one filtered-out overshoot
EX 02
triplets = [[5, 6, 7], [6, 1, 1]] · target = [5, 6, 7]
→ true
one exact match, one invalid
EX 03
triplets = [[3, 9, 2], [9, 2, 3], [1, 1, 9], [10, 1, 1]] · target = [9, 9, 9]
→ true
each coordinate maxed by a different triplet

The hintstake only what you need

HINT 1 THE NUDGE

Merging only ever raises each coordinate toward the maximum of what's picked — it can never lower one. So what disqualifies a triplet from ever being picked?

HINT 2 THE STRUCTURE

Any triplet with a coordinate above the matching target coordinate would push that coordinate past target the moment it's merged in — it's poison, permanently. Only triplets that stay under-or-equal on all three coordinates are usable.

HINT 3 ONE STEP FROM THE ANSWER

Take every usable triplet and fold them together with elementwise max — one linear pass. If the fold lands exactly on target, it's reachable; if any coordinate falls short, no combination fixes it.

Coach's boardthe pattern, step by step

FILTER, THEN FOLDPATTERN · GREEDY — ELEMENTWISE MAXtriplets = [[4,3,2],[1,8,3],[2,5,6],[9,1,1]] · target = [4, 8, 6]
4,3,2
1,8,3
2,5,6
9,1,1
BEST SO FAR
best → [0, 0, 0]
STEP 1

Target [4, 8, 6]. Merging only ever raises coordinates — any triplet that overshoots target anywhere is poison and gets skipped.

STEP 1 / 9 · ← → WORK TOO

The splitstwo paces, two languages

grind/merge-triplets-to-form-target-triplet.py — RACE PACE
LANG ▸
PACE ▸
class Solution:
    def mergeTriplets(self, triplets: List[List[int]], target: List[int]) -> bool:
        best = [0, 0, 0]
        for t in triplets:
            if t[0] <= target[0] and t[1] <= target[1] and t[2] <= target[2]:
                best[0] = max(best[0], t[0])
                best[1] = max(best[1], t[1])
                best[2] = max(best[2], t[2])
        return best == target
TIME O(N)SPACE O(1)PYTHON · RACE PACE · 9 LN

✓ CHIP-TIMEDALL 4 SOLUTIONS RAN GREEN AGAINST SELF-AUTHORED CASES IN CI · JDK 21 · CPYTHON 3.12 · NOTHING PUBLISHES RED