这题讲的是 每个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;
    }
}

 



posted on 2019-09-30 02:51  KeepAC  阅读(183)  评论(0编辑  收藏  举报