826. 安排工作以达到最大收益

首刷自解
class Solution {
public:
int maxProfitAssignment(vector<int>& difficulty, vector<int>& profit, vector<int>& worker) {
map<int, int> getProfit;
int n = difficulty.size();
int curMax = 0;
for (int i = 0; i < n; i++) {
if (getProfit.find(difficulty[i]) != getProfit.end())
getProfit[difficulty[i]] = max(getProfit[difficulty[i]],
profit[i]);
else getProfit[difficulty[i]] = profit[i];
}
sort(difficulty.begin(), difficulty.end());
for (int i = 0; i < n; i++) {
curMax = max(curMax, getProfit[difficulty[i]]);
getProfit[difficulty[i]] = curMax;
}
int ans = 0;
for (int w : worker) {
if (w < difficulty[0]) continue;
auto it = lower_bound(difficulty.begin(), difficulty.end(), w);
if (it == difficulty.end()) ans += curMax;
else if (*it > w) {
it--;
ans += getProfit[*it];
}
else ans += getProfit[*it];
}
return ans;
}
};