Skip to content

Commit 9ac4d5b

Browse files
authored
Create mice-and-cheese.cpp
1 parent 6e9c58f commit 9ac4d5b

File tree

1 file changed

+15
-0
lines changed

1 file changed

+15
-0
lines changed

C++/mice-and-cheese.cpp

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
// Time: O(n)
2+
// Space: O(1)
3+
4+
// greedy, quick select
5+
class Solution {
6+
public:
7+
int miceAndCheese(vector<int>& reward1, vector<int>& reward2, int k) {
8+
for (int i = 0; i < size(reward1); ++i) {
9+
reward1[i] -= reward2[i];
10+
}
11+
nth_element(begin(reward1), begin(reward1) + (k - 1), end(reward1), greater<int>());
12+
return accumulate(cbegin(reward1), cbegin(reward1) + k,
13+
accumulate(cbegin(reward2), cend(reward2), 0));
14+
}
15+
};

0 commit comments

Comments
 (0)