1899. Merge Triplets to Form Target Triplet - Medium
前往題目
想法
- 題目根本沒看清楚😂以為只能選兩個,結果是可以每個都選,取
max
思路
- 把不可能的去掉,也就是只要有比
target
大的那組就可以直接拋棄 - 剩下的全部取最大值看是否最終和
target
一樣,因為可以每個都取
Code
主要參考了這個解答,因為比Neetcode大大的更直觀一點
class Solution {
public boolean mergeTriplets(int[][] triplets, int[] target) {
int[] res = new int[3];
for (int[] triplet : triplets) {
// Don't want any triplet which has greater number than target
if (triplet[0] > target[0] ||
triplet[1] > target[1] ||
triplet[2] > target[2]) {
continue;
}
// Keep track the maximum
res = new int[] { Math.max(triplet[0], res[0]),
Math.max(triplet[1], res[1]),
Math.max(triplet[2], res[2]) };
}
return Arrays.equals(res, target);
}
}
1899. Merge Triplets to Form Target Triplet - Medium
https://f88083.github.io/2024/03/11/1899-Merge-Triplets-to-Form-Target-Triplet-Medium/