1899. Merge Triplets to Form Target Triplet Medium

@problem@discussion
#Array#Greedy



1/**
2 * [1899] Merge Triplets to Form Target Triplet
3 *
4 * A triplet is an array of three integers. You are given a 2D integer array triplets, where triplets[i] = [ai, bi, ci] describes the i^th triplet. You are also given an integer array target = [x, y, z] that describes the triplet you want to obtain.
5 * To obtain target, you may apply the following operation on triplets any number of times (possibly zero):
6 * 
7 * 	Choose two indices (0-indexed) i and j (i != j) and update triplets[j] to become [max(ai, aj), max(bi, bj), max(ci, cj)].
8 * 	
9 * 		For example, if triplets[i] = [2, 5, 3] and triplets[j] = [1, 7, 5], triplets[j] will be updated to [max(2, 1), max(5, 7), max(3, 5)] = [2, 7, 5].
10 * 	
11 * 	
12 * 
13 * Return true if it is possible to obtain the target triplet [x, y, z] as an element of triplets, or false otherwise.
14 *  
15 * Example 1:
16 * 
17 * Input: triplets = [[2,5,3],[1,8,4],[1,7,5]], target = [2,7,5]
18 * Output: true
19 * Explanation: Perform the following operations:
20 * - Choose the first and last triplets [<u>[2,5,3]</u>,[1,8,4],<u>[1,7,5]</u>]. Update the last triplet to be [max(2,1), max(5,7), max(3,5)] = [2,7,5]. triplets = [[2,5,3],[1,8,4],<u>[2,7,5]</u>]
21 * The target triplet [2,7,5] is now an element of triplets.
22 * 
23 * Example 2:
24 * 
25 * Input: triplets = [[3,4,5],[4,5,6]], target = [3,2,5]
26 * Output: false
27 * Explanation: It is impossible to have [3,2,5] as an element because there is no 2 in any of the triplets.
28 * 
29 * Example 3:
30 * 
31 * Input: triplets = [[2,5,3],[2,3,4],[1,2,5],[5,2,3]], target = [5,5,5]
32 * Output: true
33 * Explanation: Perform the following operations:
34 * - Choose the first and third triplets [<u>[2,5,3]</u>,[2,3,4],<u>[1,2,5]</u>,[5,2,3]]. Update the third triplet to be [max(2,1), max(5,2), max(3,5)] = [2,5,5]. triplets = [[2,5,3],[2,3,4],<u>[2,5,5]</u>,[5,2,3]].
35 * - Choose the third and fourth triplets [[2,5,3],[2,3,4],<u>[2,5,5]</u>,<u>[5,2,3]</u>]. Update the fourth triplet to be [max(2,5), max(5,2), max(5,3)] = [5,5,5]. triplets = [[2,5,3],[2,3,4],[2,5,5],<u>[5,5,5]</u>].
36 * The target triplet [5,5,5] is now an element of triplets.
37 * 
38 *  
39 * Constraints:
40 * 
41 * 	1 <= triplets.length <= 10^5
42 * 	triplets[i].length == target.length == 3
43 * 	1 <= ai, bi, ci, x, y, z <= 1000
44 * 
45 */
46pub struct Solution {}
47
48// problem: https://leetcode.com/problems/merge-triplets-to-form-target-triplet/
49// discuss: https://leetcode.com/problems/merge-triplets-to-form-target-triplet/discuss/?currentPage=1&orderBy=most_votes&query=
50
51// submission codes start here
52
53impl Solution {
54    pub fn merge_triplets(triplets: Vec<Vec<i32>>, target: Vec<i32>) -> bool {
55        false
56    }
57}
58
59// submission codes end
60
61#[cfg(test)]
62mod tests {
63    use super::*;
64
65    #[test]
66    fn test_1899() {
67    }
68}
69


Back
© 2025 bowen.ge All Rights Reserved.