Skip to content

Commit aae3fc9

Browse files
authored
Create most-profit-assigning-work.cpp
1 parent baa7002 commit aae3fc9

File tree

1 file changed

+24
-0
lines changed

1 file changed

+24
-0
lines changed

C++/most-profit-assigning-work.cpp

+24
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
// Time: O(mlogm + nlogn), m is the number of workers,
2+
// , n is the number of jobs
3+
// Space: O(n)
4+
5+
class Solution {
6+
public:
7+
int maxProfitAssignment(vector<int>& difficulty, vector<int>& profit, vector<int>& worker) {
8+
vector<pair<int,int>> jobs;
9+
for (int i = 0; i < profit.size(); ++i) {
10+
jobs.emplace_back(difficulty[i], profit[i]);
11+
}
12+
sort(jobs.begin(), jobs.end());
13+
sort(worker.begin(), worker.end());
14+
15+
int result = 0, i = 0, max_profit = 0;
16+
for (const auto& ability: worker) {
17+
while (i < profit.size() && jobs[i].first <= ability) {
18+
max_profit = max(max_profit, jobs[i++].second);
19+
}
20+
result += max_profit;
21+
}
22+
return result;
23+
}
24+
};

0 commit comments

Comments
 (0)