原题链接在这里:https://leetcode.com/problems/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 <= 10000
  • 1 <= worker.length <= 10000
  • difficulty[i], profit[i], worker[i]  are in range [1, 10^5]

题解:

Pair the difficulty and profit into job.

Sort jobs based on difficulty. And sort the worker.

Get the first worker, go through jobs from beginning and update its best profit as long as worker could accomplish job difficulity.

For the next worker, it doesn't need to iterate from beginning again, it could start from previous position, because it can do more difficult job than previous worker. So, we only need to iterate jobs once.

Like there are two pointers, one pointing at job and the other pointing at worker. For next pointer at worker, job pointer only need to move forward, not backword.

Time Complexity: O(nlogn + wlogw). n = difficulty.length. w = worker.length.

Space: O(n).

AC Java:

 class Solution {
public int maxProfitAssignment(int[] difficulty, int[] profit, int[] worker) {
int n = difficulty.length;
Job [] jobs = new Job[n];
for(int i = 0; i<n; i++){
jobs[i] = new Job(difficulty[i], profit[i]);
} Arrays.sort(jobs, (a, b) -> a.diff-b.diff);
Arrays.sort(worker);
int i = 0;
int best = 0;
int res = 0;
for(int w : worker){
while(i<n && w>=jobs[i].diff){
best = Math.max(best, jobs[i].profit);
i++;
} res += best;
} return res;
}
} class Job{
int diff;
int profit;
public Job(int diff, int profit){
this.diff = diff;
this.profit = profit;
}
}

LeetCode 826. Most Profit Assigning Work的更多相关文章

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

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

  2. 826. Most Profit Assigning Work

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

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

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

  5. Java实现 LeetCode 826 安排工作以达到最大收益(暴力DP)

    826. 安排工作以达到最大收益 有一些工作:difficulty[i] 表示第i个工作的难度,profit[i]表示第i个工作的收益. 现在我们有一些工人.worker[i]是第i个工人的能力,即该 ...

  6. All LeetCode Questions List 题目汇总

    All LeetCode Questions List(Part of Answers, still updating) 题目汇总及部分答案(持续更新中) Leetcode problems clas ...

  7. 算法与数据结构基础 - 双指针(Two Pointers)

    双指针基础 双指针(Two Pointers)是面对数组.链表结构的一种处理技巧.这里“指针”是泛指,不但包括通常意义上的指针,还包括索引.迭代器等可用于遍历的游标. 同方向指针 设定两个指针.从头往 ...

  8. Swift LeetCode 目录 | Catalog

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

  9. 【LeetCode】双指针 two_pointers(共47题)

    [3]Longest Substring Without Repeating Characters [11]Container With Most Water [15]3Sum (2019年2月26日 ...

随机推荐

  1. C++编译器会对没有构造函数的类生成默认构造函数吗?(有必要的时候才生成,要看情况。有反汇编验证)

    之前在上C++的课的时候,印象中有那么一句话:如果一个类没有任何构造函数,那么编译器会生成一个默认的构造函数 今天在看<深度探索C++对象模型>的第二章:“构造函数语意学”的时候发现之前听 ...

  2. kibana内存设置

    kibana是一个基于NodeJS的单页web应用.而NodeJS则是基于Chrome V8引擎的.V8引擎对于内存的使用是有限制的,默认情况下,64位系统下约为1.4GB,32位系统下约为0.7GB ...

  3. ubuntu系统下防火墙简单使用

    apt-get install ufw      安装防火墙sudo ufw enable|disable|status         开启/关闭/查看防火墙状态sudo ufw allow 22/ ...

  4. C# vb .net实现大小调整特效滤镜

    在.net中,如何简单快捷地实现Photoshop滤镜组中的大小调整效果呢?答案是调用SharpImage!专业图像特效滤镜和合成类库.下面开始演示关键代码,您也可以在文末下载全部源码: 设置授权 第 ...

  5. java中异常的抛出:throw throws

    java中异常的抛出:throw throws Java中的异常抛出 语法: public class ExceptionTest{ public void 方法名(参数列表) throws 异常列表 ...

  6. cs/bs架构的区别

    Client/Server是建立在局域网的基础上的,基于客户端/服务器,安全,响应快,维护难度大,不易拓展,用户面固定,需要相同的操作系统. Browser/Server是建立在广域网的基础上的,基于 ...

  7. JS-练习题

    1.foo()结果 function foo() { bar.apply(null, arguments); } function bar(){ console.log(arguments); } f ...

  8. 深入了解Cookie和Session

    会话(Session)跟踪是Web程序中常用的技术,用来跟踪用户的整个会话.常用的会话跟踪技术是Cookie与Session.Cookie通过在客户端记录信息确定用户身份,Session通过在服务器端 ...

  9. Java部分目录

    一.Java基础 1.访问权限控制 2.重载和覆盖 3.面向对象的特征 4.接口和抽象类 5.Java环境变量配置 6.Java英文缩写详解 7.如何在Maven项目中引入自己的jar包 8.使用ba ...

  10. .net core jenkins持续集成

    执行 Shell pwd ls echo ${PATH} whoami which dotnet dotnet --info dotnet --version echo '============== ...