我一直在尝试在 leetcode ( https://leetcode.com/problems/most-profit-assigning-work/ ) 上解决这个问题,其问题描述如下:
我们有工作:难度[i] 是第 i 个工作的难度,利润[i] 是第 i 个工作的利润。现在我们有一些工人。worker[i]是第i个worker的能力,也就是说这个worker最多只能完成一个有难度的工作worker[i]。
每个工人最多可以分配一项工作,但一项工作可以多次完成。例如,如果 3 个人尝试支付 1 美元的相同工作,那么总利润将为 3 美元。如果一个工人不能完成任何工作,他的利润是 0 美元。我们能赚到的最大利润是多少?
例如:输入:难度 = [2,4,6,8,10],利润 = [10,20,30,40,50],工人 = [4,5,6,7] 输出:100
解释:工人被分配难度为[4,4,6,6]的工作,他们分别获得[20,20,30,30]的利润。
我尝试使用此代码解决,
class Solution {
public:
int maxProfitAssignment(vector<int>& difficulty, vector<int>& profit, vector<int>& worker) {
map <int,int> map;
int sum = 0;
int j = 0;
// creating a hash map for profit and difficulty
for (int i = 0; i < profit.size(); i++) {
map.insert({profit[i], difficulty[i]});
}
//sorting the workers vector in descending order
sort(worker.begin(), worker.end(), greater<int>());
//Iterating through the hash map in descending order of Profit (high profit to low profit)
for (auto i = map.rbegin(); i != map.rend(); i++) {
// Assigning the job with higher profit to the workers who have difficulty
// greater than equal to the difficulty of the job
while(j < worker.size()) {
if (worker[j] >= (i->second)) {
sum += i->first;
j++;
} else {
break;
}
}
}
return sum;
}
};
但是我的代码没有通过测试用例。我将链接附加到测试用例。https://notepad.pw/r7dv12cv(这里太大贴不上)
My output : 999481607 Expected output : 999542181
我无法找到问题出在哪里。它已经通过了除此之外的所有测试用例。
如果可以,请浏览代码并在 leetcode 平台上运行。请让我知道我哪里出错了。