2611. Mice and Cheese LeetCode Solution

In this guide, you will get 2611. Mice and Cheese LeetCode Solution with the best time and space complexity. The solution to Mice and Cheese problem is provided in various programming languages like C++, Java, and Python. This will be helpful for you if you are preparing for placements, hackathons, interviews, or practice purposes. The solutions provided here are very easy to follow and include detailed explanations.

Table of Contents

  1. Problem Statement
  2. Complexity Analysis
  3. Mice and Cheese solution in C++
  4. Mice and Cheese solution in Java
  5. Mice and Cheese solution in Python
  6. Additional Resources
2611. Mice and Cheese LeetCode Solution image

Problem Statement of Mice and Cheese

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.

See also  1489. Find Critical and Pseudo-Critical Edges in Minimum Spanning Tree LeetCode Solution

Constraints:

1 <= n == reward1.length == reward2.length <= 105
1 <= reward1[i], reward2[i] <= 1000
0 <= k <= n

Complexity Analysis

  • Time Complexity: O(n\log k)
  • Space Complexity: O(k)

2611. Mice and Cheese LeetCode Solution in C++

class Solution {
 public:
  int miceAndCheese(vector<int>& reward1, vector<int>& reward2, int k) {
    // diffs[i] := reward1[i] - reward2[i].
    vector<int> diffs;

    for (int i = 0; i < reward1.size(); ++i)
      diffs.push_back(reward1[i] - reward2[i]);

    nth_element(diffs.begin(), diffs.begin() + k, diffs.end(), greater<>());
    return accumulate(reward2.begin(), reward2.end(), 0) +
           accumulate(diffs.begin(), diffs.begin() + k, 0);
  }
};
/* code provided by PROGIEZ */

2611. Mice and Cheese LeetCode Solution in Java

class Solution {
  public int miceAndCheese(int[] reward1, int[] reward2, int k) {
    Queue<Integer> minHeap = new PriorityQueue<>();

    for (int i = 0; i < reward1.length; ++i) {
      minHeap.offer(reward1[i] - reward2[i]);
      if (minHeap.size() == k + 1)
        minHeap.poll();
    }

    return Arrays.stream(reward2).sum() + minHeap.stream().mapToInt(Integer::intValue).sum();
  }
}
// code provided by PROGIEZ

2611. Mice and Cheese LeetCode Solution in Python

class Solution:
  def miceAndCheese(
      self,
      reward1: list[int],
      reward2: list[int],
      k: int,
  ) -> int:
    return (sum(reward2) +
            sum(heapq.nlargest(k, (a - b for a, b in zip(reward1, reward2)))))
# code by PROGIEZ

Additional Resources

Happy Coding! Keep following PROGIEZ for more updates and solutions.