Off-by-one on range boundaries
Wrong move: Loop endpoints miss first/last candidate.
Usually fails on: Fails on minimal arrays and exact-boundary answers.
Fix: Re-derive loops from inclusive/exclusive ranges before coding.
Move from brute-force thinking to an efficient approach using array strategy.
There are two mice and n different types of cheese, each type of cheese should be eaten by exactly one mouse.
A point of the cheese with index i (0-indexed) is:
reward1[i] if the first mouse eats it.reward2[i] if the second mouse eats it.You are given a positive integer array reward1, a positive integer array reward2, and a non-negative integer k.
Return the maximum points the mice can achieve if the first mouse eats exactly k types of cheese.
Example 1:
Input: reward1 = [1,1,3,4], reward2 = [4,4,1,1], k = 2 Output: 15 Explanation: In this example, the first mouse eats the 2nd (0-indexed) and the 3rd types of cheese, and the second mouse eats the 0th and the 1st types of cheese. The total points are 4 + 4 + 3 + 4 = 15. It can be proven that 15 is the maximum total points that the mice can achieve.
Example 2:
Input: reward1 = [1,1], reward2 = [1,1], k = 2 Output: 2 Explanation: In this example, the first mouse eats the 0th (0-indexed) and 1st types of cheese, and the second mouse does not eat any cheese. The total points are 1 + 1 = 2. It can be proven that 2 is the maximum total points that the mice can achieve.
Constraints:
1 <= n == reward1.length == reward2.length <= 1051 <= reward1[i], reward2[i] <= 10000 <= k <= nProblem summary: There are two mice and n different types of cheese, each type of cheese should be eaten by exactly one mouse. A point of the cheese with index i (0-indexed) is: reward1[i] if the first mouse eats it. reward2[i] if the second mouse eats it. You are given a positive integer array reward1, a positive integer array reward2, and a non-negative integer k. Return the maximum points the mice can achieve if the first mouse eats exactly k types of cheese.
Start with the most direct exhaustive search. That gives a correctness anchor before optimizing.
Pattern signal: Array · Greedy
[1,1,3,4] [4,4,1,1] 2
[1,1] [1,1] 2
house-robber)Source-backed implementations are provided below for direct study and interview prep.
// Accepted solution for LeetCode #2611: Mice and Cheese
class Solution {
public int miceAndCheese(int[] reward1, int[] reward2, int k) {
int n = reward1.length;
Integer[] idx = new Integer[n];
Arrays.setAll(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;
}
}
// Accepted solution for LeetCode #2611: Mice and Cheese
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
}
# Accepted solution for LeetCode #2611: Mice and Cheese
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:])
// Accepted solution for LeetCode #2611: Mice and Cheese
// Rust example auto-generated from java reference.
// Replace the signature and local types with the exact LeetCode harness for this problem.
impl Solution {
pub fn rust_example() {
// Port the logic from the reference block below.
}
}
// Reference (java):
// // Accepted solution for LeetCode #2611: Mice and Cheese
// class Solution {
// public int miceAndCheese(int[] reward1, int[] reward2, int k) {
// int n = reward1.length;
// Integer[] idx = new Integer[n];
// Arrays.setAll(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;
// }
// }
// Accepted solution for LeetCode #2611: Mice and Cheese
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;
}
Use this to step through a reusable interview workflow for this problem.
Try every possible combination of choices. With n items each having two states (include/exclude), the search space is 2ⁿ. Evaluating each combination takes O(n), giving O(n × 2ⁿ). The recursion stack or subset storage uses O(n) space.
Greedy algorithms typically sort the input (O(n log n)) then make a single pass (O(n)). The sort dominates. If the input is already sorted or the greedy choice can be computed without sorting, time drops to O(n). Proving greedy correctness (exchange argument) is harder than the implementation.
Review these before coding to avoid predictable interview regressions.
Wrong move: Loop endpoints miss first/last candidate.
Usually fails on: Fails on minimal arrays and exact-boundary answers.
Fix: Re-derive loops from inclusive/exclusive ranges before coding.
Wrong move: Locally optimal choices may fail globally.
Usually fails on: Counterexamples appear on crafted input orderings.
Fix: Verify with exchange argument or monotonic objective before committing.