Re: [闲聊] 每日leetcode

楼主: oin1104 (是oin的说)   2024-06-18 12:29:57
https://leetcode.com/problems/most-profit-assigning-work
: 826. Most Profit Assigning Work
: 你有n份工作与m个工人
: 给定三数列
: difficulty[i]与profit[i]代表第i份工作的收益与困难度
: worker[j]代表工人的能力
: 工人只能接受困难度小于等于自己能力的工作
: 每位工人只能接一份工作 但工作能被重复完成
: 请回传我们将工作分配给工人后能获得的最大收益
:  
这题用heap或是双指针都可以
然后我突然想练习看看二分搜
反正都是nlogn
只是二分搜写起来很麻烦
妈的 早就知道乖乖sort双指针了
思路:
只要sort工作
然后让每个员工找到可以做的最大工作就可以了
```cpp
class Solution {
public:
int maxProfitAssignment(vector<int>& difficulty, vector<int>& profit, vector
<int>& worker)
{
int res = 0;
int len = difficulty.size();
vector<pair<int,int>> paper;
map<int,int> paper2;
for(int i = 0 ; i < len ; i ++)
{
paper2[difficulty[i]] = max(paper2[difficulty[i]] , profit[i]);
}
for(auto k : paper2)
{
paper.push_back({k.first , k.second});
}
int nmax = 0;
len = paper.size();
for(int i = 0 ; i < len ; i ++)
{
nmax = max(nmax , paper[i].second);
paper[i].second = max(nmax , paper[i].second);
}
int wlen = worker.size();
for(int i = 0 ; i < wlen ; i ++)
{
if(worker[i] < paper[0].first)continue;
if(worker[i] > paper[len-1].first){
res += paper[len-1].second;
continue;
}
int l = 0 ;
int r = len-1;
int m = 0;
while(l < r)
{
int m = (l + r + 1) / 2;
if(paper[m].first <= worker[i])
{
l = m;
}
else
{
r = m - 1;
}
}
res += paper[l].second;
//cout << paper[l].second << " ";
}
return res;
}
};
```

Links booklink

Contact Us: admin [ a t ] ucptt.com