• LeetCode:安排工作以达到最大收益【455】


    LeetCode:安排工作以达到最大收益【455】

    题目描述

    有一些工作:difficulty[i] 表示第i个工作的难度,profit[i]表示第i个工作的收益。

    现在我们有一些工人。worker[i]是第i个工人的能力,即该工人只能完成难度小于等于worker[i]的工作。

    每一个工人都最多只能安排一个工作,但是一个工作可以完成多次。

    举个例子,如果3个工人都尝试完成一份报酬为1的同样工作,那么总收益为 $3。如果一个工人不能完成任何工作,他的收益为 $0 。

    我们能得到的最大收益是多少?

    示例:

    输入: difficulty = [2,4,6,8,10], profit = [10,20,30,40,50], worker = [4,5,6,7]
    输出: 100 
    解释: 工人被分配的工作难度是 [4,4,6,6] ,分别获得 [20,20,30,30] 的收益。

    提示:

    • 1 <= difficulty.length = profit.length <= 10000
    • 1 <= worker.length <= 10000
    • difficulty[i], profit[i], worker[i]  的范围是 [1, 10^5]

    题目分析

      

    Java题解

    class Solution {
        public int maxProfitAssignment(int[] difficulty, int[] profit, int[] worker) {
            ArrayList<Pair> jobs = new ArrayList<>();
            for(int i=0;i<difficulty.length;i++)
                jobs.add(new Pair(difficulty[i],profit[i]));
            Collections.sort(jobs, new Comparator<Pair>() {
                @Override
                public int compare(Pair o1, Pair o2) {
                    /*
                    < return -1
                    = return 0
                    > return 1
                     */
                    return o1.difficulty-o2.difficulty;
                }
            });
            Arrays.sort(worker);
            int ans=0;
            int best=0;
            int i= 0;
            for(int level:worker)
            {
                while (i<difficulty.length&&level>=jobs.get(i).difficulty)
                    best =Math.max(best,jobs.get(i++).profit);
                ans +=best;
            }
            return ans;
        }
    }
    
    class Pair{
        int difficulty;
        int profit;
        public Pair(int difficulty, int profit) {
            this.difficulty = difficulty;
            this.profit = profit;
        }
    }
    
  • 相关阅读:
    angular 中如何使用自定义组件
    angular组件数据和事件
    angular组件数据
    angular绑定数据
    angular自定义组件
    angular项目目录结构分析
    Angular 开发工具介绍
    从Microsoft.AspNet.Identity看微软推荐的一种MVC的分层架构
    EF How to use context.Set and context.Entry, which ships with EF4.1 ?
    C# 向IQueryable添加一个Include扩展方法
  • 原文地址:https://www.cnblogs.com/MrSaver/p/9500618.html
Copyright © 2020-2023  润新知