2611. 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.*
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32
| class Solution { public: int miceAndCheese(vector<int>& reward1, vector<int>& reward2, int k) { int res = 0, a = 0, b = 0; priority_queue<int> qa, qb; for(int i = 0; i < reward1.size(); i++) { if(reward1[i] > reward2[i]) { a += 1; res += reward1[i]; qa.push(reward2[i] - reward1[i]); } else { b += 1; res += reward2[i]; qb.push(reward1[i] - reward2[i]); } } if(a == k) return res; if(a < k) { while(a < k) { a += 1; res += qb.top(); qb.pop(); } } if(a > k) { while(a > k) { a -= 1; res += qa.top(); qa.pop(); } } return res; } };
|