-
Notifications
You must be signed in to change notification settings - Fork 365
/
s1.cpp
22 lines (22 loc) · 834 Bytes
/
s1.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
// OJ: https://leetcode.com/problems/most-profit-assigning-work/
// Author: github.com/lzl124631x
// Time: O(NlogN)
// Space: O(N)
class Solution {
public:
int maxProfitAssignment(vector<int>& difficulty, vector<int>& profit, vector<int>& worker) {
map<int, int, greater<int>> diffToProfit;
int N = difficulty.size(), maxProfit = 0, ans = 0;
for (int i = 0; i < N; ++i) {
diffToProfit[difficulty[i]] = max(diffToProfit[difficulty[i]], profit[i]);
}
for (auto it = diffToProfit.rbegin(); it != diffToProfit.rend(); ++it) {
it->second = maxProfit = max(maxProfit, it->second);
}
for (int w : worker) {
auto it = diffToProfit.lower_bound(w);
if (it != diffToProfit.end()) ans += it->second;
}
return ans;
}
};