原题链接在这里: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. Angular—入门环境,项目创建,导入项目

    1.安装 nodejs,npm nodejs 下载地址: https://nodejs.org/en/download/

  2. 2019 映客直播java面试笔试题 (含面试题解析)

    本人3年开发经验.18年年底开始跑路找工作,在互联网寒冬下成功拿到阿里巴巴.今日头条.映客直播等公司offer,岗位是Java后端开发,最终选择去了映客直播. 面试了很多家公司,感觉大部分公司考察的点 ...

  3. CGContextRef&CGMutablePathRef&UIBezierPath简单学习

    简单的四句介绍 Quartz是一个二维绘图引擎,使用的是CoreGraphics库,同时支持iOS和Mac系统 CGContextRef:获取图形上下文.或者叫作用域,即画布,他是专门用来保存绘画期间 ...

  4. eyoucms 模板

    https://www.oschina.net/p/eyoucms 下载模板 https://www.eyoucms.com/doc/operation/ 学习手册

  5. Vue学习之项目部分代码(十八)

    1.mian.js: // 入口文件 import Vue from "vue"; // 1.1导入路由 import VueRouter from "vue-route ...

  6. TOEFL词汇笔记英语托福英语

    conjectural-based on guessing 推测的-给予猜测的 consciously-on purpose 有意识地-有目的地 conserve-retain保存-保存 conspi ...

  7. 深入理解jvm--性能监控工具

    1.jvm监控工具介绍 1.1.jconsole JConsole是一个基于JMX的GUI工具,用于连接正在运行的JVM,不过此JVM需要使用可管理的模式启动. 1.2.启动jconsole 通过JD ...

  8. appium 操作界面

    操作界面函数: 1.swipe():模拟滑动 2.tap():点击坐标 1.swipe()函数:用来模拟滑动操作 参数说明: 坐标就是x/y坐标 duration是滑动从起点到终点坐标所耗费的时间. ...

  9. 自定义View(二),强大的Canvas

    本文转自:http://www.jcodecraeer.com/a/anzhuokaifa/androidkaifa/2012/1212/703.html Android中使用图形处理引擎,2D部分是 ...

  10. Linux LVM 逻辑卷管理

    使用Linux好久了,一定会意识到一个问题,某个分区容量不够用了,想要扩容怎么办?这里就涉及到LVM逻辑卷的管理了,可以动态调整Linux分区容量. LVM 概述 全称Logical Volume M ...