原题链接在这里: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++与C语言很像,但两个有本质的区别,C语言是面向过程的,而C++是面向对象的,下面就给大家梳理梳理. 1.C语言有标准的函数库,它们松散的,只是把功能相同的函数放在一个头文件中:而C++ ...

  2. redis源码分析(二)-rio(读写抽象层)

    Redis io抽象层 Redis中涉及到多种io,如socket与file,为了统一对它们的操作,redis设计了一个抽象层,即rio,使用rio可以实现将数据写入到不同的底层io,但是接口相同.r ...

  3. 【数据结构】12.java源码关于ConcurrentHashMap

    目录 1.ConcurrentMap的内部结构 2.ConcurrentMap构造函数 3.元素新增策略4.元素删除5.元素修改和查找6.特殊操作7.扩容8.总结 1.ConcurrentMap内部结 ...

  4. Stack实现

    栈的三种操作算法很简单 STACK-EMPTY(S) 1 if S.top == 0 2    return TRUE 3 else return FALSE PUSH(S, x) 1 S.top = ...

  5. (1)ASP.NET Core 应用启动Startup类简介

    1.前言 Core与早期版本的 ASP.NET 对比,配置应用程序的方式的 Global.asax.FilterConfig.cs和RouteConfig.cs 都被Program.cs 和 Star ...

  6. P2711 小行星 (最大流)

    题目 P2711 小行星 解析 这道题挺巧妙的,乍一看是空间上的,无从下手,稍微转换一下就可以了. 看到题目,求消除这些行星的最少次数,就是求最小割,也就是求最大流,考虑怎样建图. 考虑当我们消去一个 ...

  7. Vue学习之Webpack基本使用小结(十三)

    一.新建dist 文件夹: 二.新建src文件夹: 在其下面创建 css .js .images文件夹及 index.html.main.js(这是项目Js的主入口) 三.html中简单创建一个列表: ...

  8. 1M大概多少个字

    <?php echo strlen("你"); 保存文件为gbk 输出2 保存文件为utf-8 输出3 说明不同编码占用字节不同 1M=1024kB 1KB = 1024B ...

  9. unittest管理接口用例(数据分离-读取excel)

    1.简单读取 #coding=utf-8 #调用封装好的excel读取公共方法 from python_API.common.ReadExcel import ReadExcel import req ...

  10. WinServer-the security database on the server does not have a computer account for

    用了本地的administrator登陆