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 <= 10000
  • 1 <= worker.length <= 10000
  • difficulty[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

https://leetcode.com/problems/most-profit-assigning-work/discuss/127031/C%2B%2BJavaPython-Sort-and-Two-pointer

https://leetcode.com/problems/most-profit-assigning-work/discuss/126988/O(M-%2B-N)-solution-based-on-preprocessing

LeetCode All in One 题目讲解汇总(持续更新中...)

[LeetCode] Most Profit Assigning Work 安排最大利润的工作的更多相关文章

  1. [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 ...

  2. LeetCode 826. Most Profit Assigning Work

    原题链接在这里:https://leetcode.com/problems/most-profit-assigning-work/ 题目: We have jobs: difficulty[i] is ...

  3. 【LeetCode】826. Most Profit Assigning Work 解题报告(Python)

    作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 题目地址: https://leetcode.com/problems/most-pro ...

  4. 826. Most Profit Assigning Work

    https://leetcode.com/problems/most-profit-assigning-work/description/ class Solution { public: int m ...

  5. [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 ...

  6. [LeetCode]621. Task Scheduler 任务安排 题解

    题目描述 给定一个char数组,代表CPU需要做的任务,包含A-Z,不用考虑顺序,每个任务能在1个单位完成.但是有规定一个非负整数n代表两个相同任务之间需要至少n个时间单位.球最少数量的时间单位完成所 ...

  7. SEOer怎样安排一天的工作

    昨天一文谈到seo车型优化恐惧了,一些兄弟果断说,不玩seo.妮子不是吓人的,希望大家好好看清自己如今行业现状,怎样突破下步,如今仅仅剩下竞价和B2B付费平台了吗?每天坚持博客更新,尽管不像那些名博那 ...

  8. Swift LeetCode 目录 | Catalog

    请点击页面左上角 -> Fork me on Github 或直接访问本项目Github地址:LeetCode Solution by Swift    说明:题目中含有$符号则为付费题目. 如 ...

  9. leetcode 第188题,我的解法,Best Time to Buy and Sell Stock IV

    <span style="font-family: Arial, Helvetica, sans-serif; background-color: rgb(255, 255, 255) ...

随机推荐

  1. JAVA实现C/S结构小程序

    程序功能: 客户端向服务器发送一个本地磁盘中的文件, 服务器程序接受后保存在其他位置. 客户端实现步骤: 创建一个客户端对象Socket,构造方法中绑定服务器的IP地址 和 端口号 使用Socket对 ...

  2. 用 python 写一个年会抽奖小程序

    使用 pyinstaller 打包工具常用参数指南 pyinstaller -F demo.py 参数 含义 -F 指定打包后只生成一个exe格式的文件 -D –onedir 创建一个目录,包含exe ...

  3. [物理学与PDEs]第5章习题3 第二 Piola 应力张量的对称性

    试证明: 在物质描述下, 动量矩守恒定律等价于第二 Piola 应力张量的对称性. 证明: 由 $$\beex \bea \int_{G_t}\rho\sex{{\bf y}\times\cfrac{ ...

  4. 《11招玩转网络安全》之第一招:Docker For Docker

    玩转黑客那些工具,缺少了虚拟机怎么行,除了用虚拟机虚拟整个系统,Docker也不能缺少,读者只需要知道,Docker只虚拟Linux系统中的某个程序就可以了.本节就来介绍Linux下安装设置Docke ...

  5. Windows系统盘符错乱导致桌面无法加载。

    问题如下 : 同事有台笔记本更换SSD硬盘,IT职员帮他将新硬盘分好区后再将系统完整Ghost过来,然后装到笔记本上.理论上直接就可以使用了!但结果开机后登陆用户桌面无法显示,屏幕黑屏什么都没有. 问 ...

  6. CentOS安装VLC

    For EL7: rpm -Uvh https://dl.fedoraproject.org/pub/epel/7/x86_64/e/epel-release-7-9.noarch.rpm rpm - ...

  7. 高并发秒杀系统--Service事务管理与继承测试

    [Spring IoC的类型及应用场景]  [Spring事务使用方式] [Spring事务的特性] [Spring事务回滚的理解] [Service声明式事务的配置] 1.配置事务管理器 2.配置基 ...

  8. spring cloud(学习笔记)高可用注册中心(Eureka)的实现(一)

    最近在学习的时候,发现微服务架构中,假如只有一个注册中心,那这个注册中心挂了可怎么办,这样的系统,既不安全,稳定性也不好,网上和书上找了一会,发现这个spring cloud早就想到了,并帮我们解决了 ...

  9. 51nod--1006 最长公共子序列Lcs (动态规划)

    题目: 给出两个字符串A B,求A与B的最长公共子序列(子序列不要求是连续的). 比如两个串为: abcicba abdkscab ab是两个串的子序列,abc也是,abca也是,其中abca是这两个 ...

  10. uni-app图片压缩转base64位 利用递归来实现多张图片压缩

    //选择图片 chooseImage(){ let that =this uni.chooseImage({ sizeType: ['original','compressed'], //可以指定是原 ...