826 - Most Profit Assigning Work
JAVA
class Solution { public int maxProfitAssignment(int[] difficulty, int[] profit, int[] worker) { int n = difficulty.length; List<int[]> job = new ArrayList<>(); for (int i = 0; i < n; ++i) { job.add(new int[] {difficulty[i], profit[i]}); } job.sort(Comparator.comparing(a -> a[0])); Arrays.sort(worker); int res = 0; int i = 0, t = 0; for (int w : worker) { while (i < n && job.get(i)[0] <= w) { t = Math.max(t, job.get(i++)[1]); } res += t; } return res; } }
C++
class Solution { public: int maxProfitAssignment(vector<int>& difficulty, vector<int>& profit, vector<int>& worker) { int n = difficulty.size(); vector<pair<int, int>> job; for (int i = 0; i < n; ++i) { job.push_back({difficulty[i], profit[i]}); } sort(job.begin(), job.end()); sort(worker.begin(), worker.end()); int i = 0, t = 0; int res = 0; for (auto w : worker) { while (i < n && job[i].first <= w) { t = max(t, job[i++].second); } res += t; } return res; } };
Comments
Post a Comment