原题链接在这里: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. php中让数组顺序随机化,打乱顺序等

    php中有很多排序的函数,sort,rsort,ksort,krsort,asort,arsort,natcasesort,这些函数用来对数组的键或值进行这样,或那样的排序. 可以终究有时候还需要一些 ...

  2. day55——django引入、小型django(socket包装的服务器)

    day55 吴超老师Django总网页:https://www.cnblogs.com/clschao/articles/10526431.html 请求(网址访问,提交数据等等) request 响 ...

  3. centos 6.5安装zabbix 4.4

    一.安装环境 本环境,使用单机部署. 操作系统:centos 7.5 x64zabbix-server,Mysql,php,nginx都在同一台服务器.都是使用Yum安装的! 官方安装文档: http ...

  4. N皇后问题的python实现

    数据结构中常见的问题,最近复习到了,用python做一遍. # 检测(x,y)这个位置是否合法(不会被其他皇后攻击到) def is_attack(queue, x, y): for i in ran ...

  5. Kibana访问报错

    浏览器访问提示:Kibana server is not ready yet 查看日志如下 {"type":"log","@timestamp&quo ...

  6. 《JAVA高并发编程详解》-七种单例模式

  7. PHP的序列化、对象、反射、异常与错误

    1. 怎么理解php里面的序列化与反序列化? 序列化是将对象转换为字节流.反序列化就是将流转换为对象. 这两个过程结合起来,可以轻松地存储和传输数据,在网络中可以做到跨平台.快速传输. 两种序列化方式 ...

  8. C#下IOC/依赖注入框架Grace介绍

    对依赖注入或控制反转不了解的童鞋请先自行学习一下这一设计,这里直接介绍项目和实现步骤. Grace是一个开源.轻巧.易用同时特性丰富.性能优秀的依赖注入容器框架.从这篇IOC容器评测文章找到的Grac ...

  9. js运算符及数据类型转换(二)

    1.一元运算符+.-[将其它类型转化为number类型,相当于调用了Number()函数]var num = +('hello')  NaN  typeof num->numbernum = + ...

  10. Java 之 Session

    Session 一.概述 Session技术:服务器端会话技术,在一次会话的多次请求间共享数据,将数据保存在服务器端的对象(HttpSession)中. 二.使用步骤 1.获取 HttpSession ...