826. Most Profit Assigning Work

https://leetcode.com/problems/most-profit-assigning-work/description/

class Solution {
public:
    int maxProfitAssignment(vector<int>& difficulty, vector<int>& profit, vector<int>& worker) {
        int res = 0;
        
        vector<pair<int,int>> jobs(difficulty.size());
        for (int i = 0; i < difficulty.size(); i++)
            jobs[i] = { difficulty[i], profit[i] };
        sort(jobs.begin(), jobs.end());
        
        sort(worker.begin(), worker.end());
        
        int i = 0, j = 0, p = 0;
        while (j < worker.size()) {
            while (i < jobs.size() && jobs[i].first <= worker[j]) {
                p = max(p, jobs[i].second);
                i++;
            }
            res += p;
            j++;
        }
        return res;
    }
};

 

posted @ 2018-05-15 12:37  JTechRoad  阅读(218)  评论(0编辑  收藏  举报