File tree Expand file tree Collapse file tree 1 file changed +37
-0
lines changed Expand file tree Collapse file tree 1 file changed +37
-0
lines changed Original file line number Diff line number Diff line change
1
+ class Solution {
2
+ public:
3
+ int maxProfitAssignment (vector<int >& difficulty, vector<int >& profit, vector<int >& worker) {
4
+ std::sort (worker.begin (), worker.end ());
5
+ vector<int > indices (difficulty.size ());
6
+ for (auto i = 0 ; i < indices.size (); i++){
7
+ indices[i] = i;
8
+ }
9
+ std::sort (indices.begin (), indices.end (), [&profit](int a, int b){
10
+ return profit[a] < profit[b];
11
+ });
12
+ vector<int > sortedDiff (indices.size ());
13
+ vector<int > sortedProf (indices.size ());
14
+ for (auto i = 0 ; i < indices.size (); i++){
15
+ sortedDiff[i] = difficulty[indices[i]];
16
+ sortedProf[i] = profit[indices[i]];
17
+ }
18
+ for (auto i = 0 ; i < sortedDiff.size ()-1 ; i++){
19
+ if (sortedDiff[i] >= sortedDiff[i+1 ]){
20
+ sortedDiff.erase (sortedDiff.begin () + i);
21
+ sortedProf.erase (sortedProf.begin () + i);
22
+ i = max (i-2 , 0 );
23
+ }
24
+ }
25
+ auto sum = 0 ;
26
+ auto curIndex = 0 ;
27
+ for (auto i = 0 ; i < worker.size (); i++){
28
+ while ( curIndex+1 <sortedDiff.size () && worker[i] >= sortedDiff[curIndex+1 ]){
29
+ curIndex++;
30
+ }
31
+ if (worker[i] >= sortedDiff[curIndex]){
32
+ sum += sortedProf[curIndex];
33
+ }
34
+ }
35
+ return sum;
36
+ }
37
+ };
You can’t perform that action at this time.
0 commit comments