LC.P2611[老鼠和奶酪]

方法一:贪心+排序

1
2
3
4
5
6
7
8
9
10
11
12
13
14
class Solution {
public int miceAndCheese(int[] reward1, int[] reward2, int k) {
int ans = 0, 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;
}
}
  • 时间复杂度:$O(nlogn)$
  • 空间复杂度:$O(n)$