Skip to content

Commit b3835eb

Browse files
authored
ac 826
1 parent 92c1efd commit b3835eb

File tree

1 file changed

+37
-0
lines changed

1 file changed

+37
-0
lines changed

826.cpp

Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
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+
};

0 commit comments

Comments
 (0)