码迷,mamicode.com
首页 > 其他好文 > 详细

826. Most Profit Assigning Work

时间:2019-09-30 09:47:44      阅读:58      评论:0      收藏:0      [点我收藏+]

标签:NPU   example   and   output   array   length   不同   get   难度   

这题讲的是 每个worker 只能做比他能力难度小的或者一样的工作, 每个工作创造的价值不同,为workers 们能创造的最高价值。

比如worker0 = 4,   diff = {1,3,4}  profit=   {100,10,10} 那这个worker 显然要挑选 100这个高的profit 去做。 所以一句话:work 要挑选能力范围内,尽可能高价值的事去做。

 

所以要排序,难度从低到高。 worker 也得排序,然后找出自己能力范围内 max profit, 下一个人在这个 max profit 基础上继续寻找max profit. 

 

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.


唯一的难度在于排序,要对 difficulty 和 profit 同时排序,建立一个Node, 然后调用排序函数。
class Node{
    int diff;
    int profit;
    Node(int diff, int profit){
        this.diff = diff;
        this.profit = profit;
    }
}

class Solution {
    public int maxProfitAssignment(int[] difficulty, int[] profit, int[] worker) {
        int N = difficulty.length;
        Node[] nodes = new Node[N];
        for(int i=0; i<N; i++){
            nodes[i] = new Node(difficulty[i],profit[i]) ;
        }
        
        Arrays.sort(nodes,(o1,o2)->(o1.diff-o2.diff));
        Arrays.sort(worker);
        int max = 0;    
        int index =0;
        int sum = 0;
    
        for(int i=0; i<worker.length; i++ ){

            while(index<N && worker[i] >= nodes[index].diff){
                max = Math.max(nodes[index].profit,max);
                index++;
            }
            sum+=max;
        }
        
        return sum;
    }
}

 



826. Most Profit Assigning Work

标签:NPU   example   and   output   array   length   不同   get   难度   

原文地址:https://www.cnblogs.com/keepAC/p/11610753.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!