Mice and Cheese - Problem

Imagine you have two hungry mice and n different types of delicious cheese spread out before them. Each cheese must be eaten by exactly one mouse - no sharing allowed!

Here's the twist: each mouse has different taste preferences! For cheese i, the first mouse gets reward1[i] points of satisfaction, while the second mouse gets reward2[i] points.

Your goal: Distribute the cheese optimally so that the first mouse eats exactly k types of cheese, and the total satisfaction points are maximized.

Example: If reward1 = [1,1,3,4], reward2 = [4,4,1,1], and k = 2, the optimal strategy is to give cheese types 2 and 3 to the first mouse (earning 3+4=7 points) and types 0 and 1 to the second mouse (earning 4+4=8 points), for a total of 15 points.

Input & Output

example_1.py โ€” Basic Example
$ Input: reward1 = [1,1,3,4], reward2 = [4,4,1,1], k = 2
โ€บ Output: 15
๐Ÿ’ก Note: The advantages are [-3,-3,2,3]. After sorting by advantage: cheese 3 (adv +3), cheese 2 (adv +2), cheese 0 (adv -3), cheese 1 (adv -3). Mouse 1 gets cheese 3 and 2 (4+3=7 points), Mouse 2 gets cheese 0 and 1 (4+4=8 points). Total = 15.
example_2.py โ€” All Positive Advantages
$ Input: reward1 = [1,1], reward2 = [1,1], k = 1
โ€บ Output: 2
๐Ÿ’ก Note: Both cheese have advantage 0, so it doesn't matter which one mouse 1 gets. Mouse 1 gets 1 cheese (1 point), Mouse 2 gets 1 cheese (1 point). Total = 2.
example_3.py โ€” Edge Case k=0
$ Input: reward1 = [1,2,3], reward2 = [3,2,1], k = 0
โ€บ Output: 6
๐Ÿ’ก Note: Mouse 1 gets no cheese (k=0), so Mouse 2 gets all cheese types and earns 3+2+1=6 points.

Constraints

  • 1 โ‰ค n โ‰ค 105
  • 1 โ‰ค reward1[i], reward2[i] โ‰ค 1000
  • 0 โ‰ค k โ‰ค n
  • Each cheese must be eaten by exactly one mouse

Visualization

Tap to expand
Advantage-Based Decision Making๐Ÿง€ Available Cheese Types:Type 0M1: 1ptM2: 4ptAdv: -3Type 1M1: 1ptM2: 4ptAdv: -3Type 2M1: 3ptM2: 1ptAdv: +2Type 3M1: 4ptM2: 1ptAdv: +3๐Ÿ“Š Priority Ranking (by advantage):1st: Type 3Advantage: +3โ†’ Give to Mouse 12nd: Type 2Advantage: +2โ†’ Give to Mouse 13rd: Type 0Advantage: -3โ†’ Give to Mouse 24th: Type 1Advantage: -3โ†’ Give to Mouse 2๐ŸŽฏ Final Assignment (k=2):๐Ÿญ Mouse 1 Gets:Type 3: 4 pointsType 2: 3 pointsSubtotal: 7 points๐Ÿญ Mouse 2 Gets:Type 0: 4 pointsType 1: 4 pointsSubtotal: 8 pointsTotal Score: 15
Understanding the Visualization
1
Calculate Opportunity Costs
For each cheese, calculate what we gain/lose by giving it to mouse 1 instead of mouse 2
2
Rank by Priority
Sort all cheese types by their advantage values - highest advantage first
3
Make Greedy Choices
Assign the k most advantageous cheese types to mouse 1, rest to mouse 2
Key Takeaway
๐ŸŽฏ Key Insight: By thinking in terms of 'advantage' or 'opportunity cost', we can make locally optimal decisions that lead to the globally optimal solution. This greedy approach works because we're maximizing the total benefit across both mice.
Asked in
Google 42 Amazon 35 Microsoft 28 Meta 22
47.2K Views
Medium Frequency
~15 min Avg. Time
1.8K Likes
Ln 1, Col 1
Smart Actions
๐Ÿ’ก Explanation
AI Ready
๐Ÿ’ก Suggestion Tab to accept Esc to dismiss
// Output will appear here after running code
Code Editor Closed
Click the red button to reopen