Skip to content

Leetcode 1899. Merge Triplets to Form Target Triplet #245

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
Woodyiiiiiii opened this issue Apr 25, 2023 · 0 comments
Open

Leetcode 1899. Merge Triplets to Form Target Triplet #245

Woodyiiiiiii opened this issue Apr 25, 2023 · 0 comments

Comments

@Woodyiiiiiii
Copy link
Owner

1899. Merge Triplets to Form Target Triplet

1899. Merge Triplets to Form Target Triplet

我没注意贪心,想着是排序然后一个一个比较。

但明确两点:

  1. 一旦三元组其中一个数大于目标数,就不能交换了/也不能取
  2. 在目标数范围内尽量取最值

所以这是贪心Greedy

class Solution {
    public boolean mergeTriplets(int[][] triplets, int[] target) {
        int[] res = new int[3];
        for (int[] triplet : triplets) {
            if (triplet[0] <= target[0] && triplet[1] <= target[1] && triplet[2] <= target[2]) {
                res[0] = Math.max(res[0], triplet[0]);
                res[1] = Math.max(res[1], triplet[1]);
                res[2] = Math.max(res[2], triplet[2]);
            }
        }
        // res equals target?
        return Arrays.equals(res, target);
    }
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant