[LeetCode] 1899. Merge Triplets to Form Target Triplet

Posted CNoodle

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了[LeetCode] 1899. Merge Triplets to Form Target Triplet相关的知识,希望对你有一定的参考价值。

A triplet is an array of three integers. You are given a 2D integer array triplets, where triplets[i] = [ai, bi, ci] describes the ith triplet. You are also given an integer array target = [x, y, z] that describes the triplet you want to obtain.

To obtain target, you may apply the following operation on triplets any number of times (possibly zero):

  • 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)].
    • 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].

Return true if it is possible to obtain the target triplet [x, y, z] as an element of triplets, or false otherwise.

Example 1:

Input: triplets = [[2,5,3],[1,8,4],[1,7,5]], target = [2,7,5]
Output: true
Explanation: Perform the following operations:
- Choose the first and last triplets [[2,5,3],[1,8,4],[1,7,5]]. 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],[2,7,5]]
The target triplet [2,7,5] is now an element of triplets.

Example 2:

Input: triplets = [[1,3,4],[2,5,8]], target = [2,5,8]
Output: true
Explanation: The target triplet [2,5,8] is already an element of triplets.

Example 3:

Input: triplets = [[2,5,3],[2,3,4],[1,2,5],[5,2,3]], target = [5,5,5]
Output: true
Explanation: Perform the following operations:
- Choose the first and third triplets [[2,5,3],[2,3,4],[1,2,5],[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],[2,5,5],[5,2,3]].
- Choose the third and fourth triplets [[2,5,3],[2,3,4],[2,5,5],[5,2,3]]. 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],[5,5,5]].
The target triplet [5,5,5] is now an element of triplets.

Example 4:

Input: triplets = [[3,4,5],[4,5,6]], target = [3,2,5]
Output: false
Explanation: It is impossible to have [3,2,5] as an element because there is no 2 in any of the triplets.

Constraints:

  • 1 <= triplets.length <= 105
  • triplets[i].length == target.length == 3
  • 1 <= ai, bi, ci, x, y, z <= 1000

合并若干三元组以形成目标三元组。

三元组 是一个由三个整数组成的数组。给你一个二维整数数组 triplets ,其中 triplets[i] = [ai, bi, ci] 表示第 i 个 三元组 。同时,给你一个整数数组 target = [x, y, z] ,表示你想要得到的 三元组 。

为了得到 target ,你需要对 triplets 执行下面的操作 任意次(可能 零 次):

选出两个下标(下标 从 0 开始 计数)i 和 j(i != j),并 更新 triplets[j] 为 [max(ai, aj), max(bi, bj), max(ci, cj)] 。
例如,triplets[i] = [2, 5, 3] 且 triplets[j] = [1, 7, 5],triplets[j] 将会更新为 [max(2, 1), max(5, 7), max(3, 5)] = [2, 7, 5] 。
如果通过以上操作我们可以使得目标 三元组 target 成为 triplets 的一个 元素 ,则返回 true ;否则,返回 false 。

来源:力扣(LeetCode)
链接:https://leetcode-cn.com/problems/merge-triplets-to-form-target-triplet
著作权归领扣网络所有。商业转载请联系官方授权,非商业转载请注明出处。

思路是贪心,具体做法就是遍历 triplets,然后三元组的每一元都取最大值,看看能否凑出最后的target。同时注意如果遍历的时候遇到某个 triplet 的任何一元大于 target 中对应的那一元的时候,这个 triplet 就可以跳过了。

时间O(n)

空间O(1)

Java实现

 1 class Solution {
 2     public boolean mergeTriplets(int[][] triplets, int[] target) {
 3         int[] res = new int[3];
 4         for (int[] t : triplets) {
 5             if (t[0] <= target[0] && t[1] <= target[1] && t[2] <= target[2]) {
 6                 res = new int[] { Math.max(res[0], t[0]), Math.max(res[1], t[1]), Math.max(res[2], t[2]) };
 7             }
 8         }
 9         return Arrays.equals(res, target);
10     }
11 }

 

LeetCode 题目总结

以上是关于[LeetCode] 1899. Merge Triplets to Form Target Triplet的主要内容,如果未能解决你的问题,请参考以下文章

leetcode-Merge k Sorted Lists

#Leetcode# 56. Merge Intervals

Leetcode-88. Merge Sorted Array

#Leetcode# 88. Merge Sorted Array

LeetCode56 Merge Intervals

[Leetcode] Merge Sorted Array