[LeetCode] Most Profit Assigning Work 安排最大利润的工作
We have jobs: difficulty[i] is the difficulty of the ith job, and profit[i] is the profit of the ith job.
Now we have some workers. worker[i] is the ability of the ith worker, which means that this worker can only complete a job with difficulty at most worker[i].
Every worker can be assigned at most one job, but one job can be completed multiple times.
For example, if 3 people attempt the same job that pays $1, then the total profit will be $3. If a worker cannot complete any job, his profit is $0.
What is the most profit we can make?
Example 1:
Input: difficulty = [2,4,6,8,10], profit = [10,20,30,40,50], worker = [4,5,6,7]
Output: 100
Explanation: Workers are assigned jobs of difficulty [4,4,6,6] and they get profit of [20,20,30,30] seperately.
Notes:
1 <= difficulty.length = profit.length <= 100001 <= worker.length <= 10000difficulty[i], profit[i], worker[i]are in range[1, 10^5]
这道题给了我们一堆工作,每个工作有不同的难度,且对应着不同的利润。现在还有一些工人,每个人能胜任工作的难度不同,题目说了没人最多只能干一项工作,但是每个工作可以被多个人做。现在问我们这些工人能产生的最大利润。题目中给了一个例子,但是这个例子会给我们一些错觉,实际上difficulty数组不一定是有序的,而且可能有相同难度的工作对应不同的利润。还有就是,难度大的工作不一定利润就大,忽略了这些隐藏条件,很容易做错。为了快速的知道每个工作的难度和其对应的利润,我们可以建立难度和利润的之间映射,由于前面说了,相同的难度可能有不同的利润,所以我们希望难度映射到最高的利润,所以每次都跟自身的映射值比较一下,保留较大值即可。同时,我们还希望工作的难度能排个序,这样就可以根据工人的能力值来快速搜索能做的工作了,所以可以使用TreeMap。但是,还有个问题,前面说了难度大的工作不一定利润就大,所以我们希望难度映射的利润,是不大于其难度的工作的利润中的最大值,所以我们还要遍历一遍所有的映射,维护一个当前最大值cur,然后不断的更新每个工作的利润,同时也更新当前最大值。这些都建立好了后,之后就简单了,我们遍历每个工人,根据其能力值,来二分查第一个难度值大于该能力值的工作,可以用内置的 upper_bound 函数,如果结果第一个数字,那么我们将其前面一个难度的工作的利润加入结果res即可,参见代码如下:
解法一:
class Solution {
public:
int maxProfitAssignment(vector<int>& difficulty, vector<int>& profit, vector<int>& worker) {
int res = , cur = ;
map<int, int> m;
for (int i = ; i < difficulty.size(); ++i) {
m[difficulty[i]] = max(m[difficulty[i]], profit[i]);
}
for (auto &a : m) {
a.second = max(a.second, cur);
cur = a.second;
}
for (int i = ; i < worker.size(); ++i) {
auto it = m.upper_bound(worker[i]);
if (it != m.begin()) {
res += (--it)->second;
}
}
return res;
}
};
我们也可以不用TreeMap,而是将难度和利润组成pair,加入到一个数组中,那么就算相同的难度对应不同的利润,也不会丢失数据。我们还是根据难度给所有的工作排个序,同时,我们按能力也给工人排个序,从能力低的工人开始分配工作,这样我们只需要遍历一次所有的工作,因为当能力低的工人分配了某个工作时,后面能力高的工人不需要再考虑之前的工作,因为工作已经按难度排好序了,只需要从当前位置继续往后面匹配工作,我们可以使用一个全局变量i,当工人的能力值大于等于某个工作的难度时,我们用其来更新curMax,这里的curMax记录当前工人能做的工作的最大利润,这样当工作遍历完了,或者当前工作难度已经大于工人的能力值了时就停止,这样curMax就是该工人能获得的利润,加入结果res,参见代码如下:
解法二:
class Solution {
public:
int maxProfitAssignment(vector<int>& difficulty, vector<int>& profit, vector<int>& worker) {
int res = , n = profit.size(), curMax = , i = ;
vector<pair<int, int>> jobs;
for (int j = ; j < n; ++j) {
jobs.push_back({difficulty[j], profit[j]});
}
sort(jobs.begin(), jobs.end());
sort(worker.begin(), worker.end());
for (int ability : worker) {
while (i < n && ability >= jobs[i].first) {
curMax = max(curMax, jobs[i++].second);
}
res += curMax;
}
return res;
}
};
我们还可以使用动态规划Dynamic Programming来做,建立一个一维的dp数组,这里的dp[i]表示能力为i的工人所能获得的最大的利润。dp数组大小初始化为100001,这是题目中给定的范围,然后我们用给定的工作的难度和利润来更新dp数组,跟解法一中的更新TreeMap有些像,要考虑相同的难度可能有不同的利润,所以每次更新的时候要跟本身比较。之后就是从1开始更新dp本身,dp[i]跟前面一个dp值比较,取较大值更新dp[i],这个操作跟解法一中的那个更新TreeMap的映射值的操作是一样的,都是为了避免难度高的工作利润低。这里的dp数组cover了所有的能力值的范围,这样对于任何一个工人,我们可以根据其能力值马上找出其可以获得的最大利润,而不用像解法一中要使用二分查找,我们牺牲了空间,换来了常数级的查找速度,参见代码如下:
解法三:
class Solution {
public:
int maxProfitAssignment(vector<int>& difficulty, vector<int>& profit, vector<int>& worker) {
int res = , n = profit.size();
vector<int> dp();
for (int i = ; i < n; ++i) {
dp[difficulty[i]] = max(dp[difficulty[i]], profit[i]);
}
for (int i = ; i < dp.size(); ++i) {
dp[i] = max(dp[i], dp[i - ]);
}
for (int ability : worker) {
res += dp[ability];
}
return res;
}
};
参考资料:
https://leetcode.com/problems/most-profit-assigning-work/
https://leetcode.com/problems/most-profit-assigning-work/discuss/127133/Java-Solution-with-TreeMap
LeetCode All in One 题目讲解汇总(持续更新中...)
[LeetCode] Most Profit Assigning Work 安排最大利润的工作的更多相关文章
- [Swift]LeetCode826. 安排工作以达到最大收益 | Most Profit Assigning Work
We have jobs: difficulty[i] is the difficulty of the ith job, and profit[i] is the profit of the ith ...
- LeetCode 826. Most Profit Assigning Work
原题链接在这里:https://leetcode.com/problems/most-profit-assigning-work/ 题目: We have jobs: difficulty[i] is ...
- 【LeetCode】826. Most Profit Assigning Work 解题报告(Python)
作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 题目地址: https://leetcode.com/problems/most-pro ...
- 826. Most Profit Assigning Work
https://leetcode.com/problems/most-profit-assigning-work/description/ class Solution { public: int m ...
- [LeetCode] 207. Course Schedule 课程安排
There are a total of n courses you have to take, labeled from 0 to n - 1. Some courses may have prer ...
- [LeetCode]621. Task Scheduler 任务安排 题解
题目描述 给定一个char数组,代表CPU需要做的任务,包含A-Z,不用考虑顺序,每个任务能在1个单位完成.但是有规定一个非负整数n代表两个相同任务之间需要至少n个时间单位.球最少数量的时间单位完成所 ...
- SEOer怎样安排一天的工作
昨天一文谈到seo车型优化恐惧了,一些兄弟果断说,不玩seo.妮子不是吓人的,希望大家好好看清自己如今行业现状,怎样突破下步,如今仅仅剩下竞价和B2B付费平台了吗?每天坚持博客更新,尽管不像那些名博那 ...
- Swift LeetCode 目录 | Catalog
请点击页面左上角 -> Fork me on Github 或直接访问本项目Github地址:LeetCode Solution by Swift 说明:题目中含有$符号则为付费题目. 如 ...
- leetcode 第188题,我的解法,Best Time to Buy and Sell Stock IV
<span style="font-family: Arial, Helvetica, sans-serif; background-color: rgb(255, 255, 255) ...
随机推荐
- PyQt5开发环境搭建
一 写在开头1.1 本节内容开个新坑—“PyQt5系列”,慢慢填.本文主要内容为PyQt5开发环境的搭建. 注意:PyQt 5.10以上的版本在Python 3.6中有BUG,PyQt 5.10版本是 ...
- 抓包工具Charles基本用法
我们在进行B/S架构的Web项目开发时,在前端页面与后台交互的调试的时候,通常使用在JSP中加入“debugger;”断点,然后使用浏览器的F12开发者工具来查看可能出错的地方的数据.或者使用Http ...
- django - 总结 - ModelForm
gender = forms.ChoiceField(choices=((1, '男'), (2, '女'), (3, '其他'))) # 与sql没关系 publish = forms.Choice ...
- Java String相关
一.String类的常用方法 1. int indexOf(String s) 字符串查找 2. int lastIndexOf(String str) 3. char charAt(int inde ...
- C#利用VUDP.cs开发网络通讯应用例程
VClassLib-CS项目Github地址:https://github.com/velscode/VClassLib-CS VUDP文档地址:https://github.com/velscode ...
- vue组件化的应用
前言:vue组件化的应用涉及到vue-cli的内容,所以在应用之前是需要安装node和vue-cli的,具体如何安装我就不一一赘述了.可能一会儿我心情好的时候,可以去整理一下. 1.应用的内容:在一个 ...
- 第三节,目标检测---R-CNN网络系列
1.目标检测 检测图片中所有物体的 类别标签 位置(最小外接矩形/Bounding box) 区域卷积神经网络R-CNN 模块进化史 2.区域卷积神经网络R-CNN Region proposals+ ...
- 【原创】大数据基础之词频统计Word Count
对文件进行词频统计,是一个大数据领域的hello word级别的应用,来看下实现有多简单: 1 Linux单机处理 egrep -o "\b[[:alpha:]]+\b" test ...
- redis-hash
Hash操作,redis中Hash在内存中的存储格式如下图: hset(name, key, value) # name对应的hash中设置一个键值对(不存在,则创建:否则,修改) # 参数: # n ...
- Jenkins的详细安装及使用--windows
操作环境:Windows 一.环境准备 1 安装JDK 本文采用jdk-8u111-windows-x64.exe: 2 配置tomcat 本文采用tomcat8,无需安装,配置JAVA_HOME及J ...