comments | difficulty | edit_url | rating | source | tags | ||||
---|---|---|---|---|---|---|---|---|---|
true |
中等 |
1663 |
第 339 场周赛 Q3 |
|
有两只老鼠和 n
块不同类型的奶酪,每块奶酪都只能被其中一只老鼠吃掉。
下标为 i
处的奶酪被吃掉的得分为:
- 如果第一只老鼠吃掉,则得分为
reward1[i]
。 - 如果第二只老鼠吃掉,则得分为
reward2[i]
。
给你一个正整数数组 reward1
,一个正整数数组 reward2
,和一个非负整数 k
。
请你返回第一只老鼠恰好吃掉 k
块奶酪的情况下,最大 得分为多少。
示例 1:
输入:reward1 = [1,1,3,4], reward2 = [4,4,1,1], k = 2 输出:15 解释:这个例子中,第一只老鼠吃掉第 2 和 3 块奶酪(下标从 0 开始),第二只老鼠吃掉第 0 和 1 块奶酪。 总得分为 4 + 4 + 3 + 4 = 15 。 15 是最高得分。
示例 2:
输入:reward1 = [1,1], reward2 = [1,1], k = 2 输出:2 解释:这个例子中,第一只老鼠吃掉第 0 和 1 块奶酪(下标从 0 开始),第二只老鼠不吃任何奶酪。 总得分为 1 + 1 = 2 。 2 是最高得分。
提示:
1 <= n == reward1.length == reward2.length <= 105
1 <= reward1[i], reward2[i] <= 1000
0 <= k <= n
我们可以先将所有奶酪分给第二只老鼠,因此初始得分为
接下来,考虑将其中
因此,我们将奶酪按照
时间复杂度
相似题目:
class Solution:
def miceAndCheese(self, reward1: List[int], reward2: List[int], k: int) -> int:
n = len(reward1)
idx = sorted(range(n), key=lambda i: reward1[i] - reward2[i], reverse=True)
return sum(reward1[i] for i in idx[:k]) + sum(reward2[i] for i in idx[k:])
class Solution {
public int miceAndCheese(int[] reward1, int[] reward2, int k) {
int n = reward1.length;
Integer[] idx = new Integer[n];
for (int i = 0; i < n; ++i) {
idx[i] = i;
}
Arrays.sort(idx, (i, j) -> reward1[j] - reward2[j] - (reward1[i] - reward2[i]));
int ans = 0;
for (int i = 0; i < k; ++i) {
ans += reward1[idx[i]];
}
for (int i = k; i < n; ++i) {
ans += reward2[idx[i]];
}
return ans;
}
}
class Solution {
public:
int miceAndCheese(vector<int>& reward1, vector<int>& reward2, int k) {
int n = reward1.size();
vector<int> idx(n);
iota(idx.begin(), idx.end(), 0);
sort(idx.begin(), idx.end(), [&](int i, int j) { return reward1[j] - reward2[j] < reward1[i] - reward2[i]; });
int ans = 0;
for (int i = 0; i < k; ++i) {
ans += reward1[idx[i]];
}
for (int i = k; i < n; ++i) {
ans += reward2[idx[i]];
}
return ans;
}
};
func miceAndCheese(reward1 []int, reward2 []int, k int) (ans int) {
n := len(reward1)
idx := make([]int, n)
for i := range idx {
idx[i] = i
}
sort.Slice(idx, func(i, j int) bool {
i, j = idx[i], idx[j]
return reward1[j]-reward2[j] < reward1[i]-reward2[i]
})
for i := 0; i < k; i++ {
ans += reward1[idx[i]]
}
for i := k; i < n; i++ {
ans += reward2[idx[i]]
}
return
}
function miceAndCheese(reward1: number[], reward2: number[], k: number): number {
const n = reward1.length;
const idx = Array.from({ length: n }, (_, i) => i);
idx.sort((i, j) => reward1[j] - reward2[j] - (reward1[i] - reward2[i]));
let ans = 0;
for (let i = 0; i < k; ++i) {
ans += reward1[idx[i]];
}
for (let i = k; i < n; ++i) {
ans += reward2[idx[i]];
}
return ans;
}
class Solution:
def miceAndCheese(self, reward1: List[int], reward2: List[int], k: int) -> int:
for i, x in enumerate(reward2):
reward1[i] -= x
reward1.sort(reverse=True)
return sum(reward2) + sum(reward1[:k])
class Solution {
public int miceAndCheese(int[] reward1, int[] reward2, int k) {
int ans = 0;
int n = reward1.length;
for (int i = 0; i < n; ++i) {
ans += reward2[i];
reward1[i] -= reward2[i];
}
Arrays.sort(reward1);
for (int i = 0; i < k; ++i) {
ans += reward1[n - i - 1];
}
return ans;
}
}
class Solution {
public:
int miceAndCheese(vector<int>& reward1, vector<int>& reward2, int k) {
int n = reward1.size();
int ans = 0;
for (int i = 0; i < n; ++i) {
ans += reward2[i];
reward1[i] -= reward2[i];
}
sort(reward1.rbegin(), reward1.rend());
ans += accumulate(reward1.begin(), reward1.begin() + k, 0);
return ans;
}
};
func miceAndCheese(reward1 []int, reward2 []int, k int) (ans int) {
for i, x := range reward2 {
ans += x
reward1[i] -= x
}
sort.Ints(reward1)
n := len(reward1)
for i := 0; i < k; i++ {
ans += reward1[n-i-1]
}
return
}
function miceAndCheese(reward1: number[], reward2: number[], k: number): number {
const n = reward1.length;
let ans = 0;
for (let i = 0; i < n; ++i) {
ans += reward2[i];
reward1[i] -= reward2[i];
}
reward1.sort((a, b) => b - a);
for (let i = 0; i < k; ++i) {
ans += reward1[i];
}
return ans;
}