Most Profit Assigning Work LeetCode solution in C++

1
class Solution {
2
public:
3
static bool cmp(pair<int, int> a, pair<int, int> b) {
4
if (a.first == b.first) {
5
return a.second < b.second;
6
}
7

8
return a.first > b.first;
9
}
10
int maxProfitAssignment(vector<int> &difficulty, vector<int> &profit, vector<int> &worker) {
11
int ans = 0;
12
vector<pair<int, int>> vp;
13
for (int i = 0; i < profit.size(); i++) {
14
vp.push_back({profit[i], difficulty[i]});
15
}
16
sort(vp.begin(), vp.end(), cmp);
17
sort(worker.begin(), worker.end(), greater<int>());
18

19
// i is for traversing the vp and j is for traversing the worker
20
int i = 0, j = 0;
21

22
while (i < vp.size() and j < worker.size()) {
23
if (worker[j] >= vp[i].second) {
24
ans += vp[i].first;
25
j++;
26
} else {
27
i++;
28
}
29
}
30

31
return ans;
32
}
33
};

0

WPM •0 •0

100%

ACC •0 •0

0s

TIME •0