#1899
Medium Algorithms Merge triplets to form target triplet
Array Greedy
68.9% acceptance
Feb 25, 2026
923
80
Given a 2D triplet array and a target triplet [x,y,z], repeatedly pick two triplets and replace one with element-wise max. Return true if target can be obtained.
Key insight: Only merge triplets whose all elements are <= respective target elements. Then check if the merged result equals target.
Solution
Rust
Time O(n)
Space O(1)
impl Solution {
pub fn merge_triplets(triplets: Vec<Vec<i32>>, target: Vec<i32>) -> bool {
let (x, y, z) = (target[0], target[1], target[2]);
let mut res = [0i32; 3];
for t in &triplets {
if t[0] <= x && t[1] <= y && t[2] <= z {
res[0] = res[0].max(t[0]);
res[1] = res[1].max(t[1]);
res[2] = res[2].max(t[2]);
}
}
res[0] == x && res[1] == y && res[2] == z
}
}