zoukankan      html  css  js  c++  java
  • 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;
        }
    }
    
  • 相关阅读:
    leetcode 279. Perfect Squares
    leetcode 546. Remove Boxes
    leetcode 312. Burst Balloons
    leetcode 160. Intersection of Two Linked Lists
    leetcode 55. Jump Game
    剑指offer 滑动窗口的最大值
    剑指offer 剪绳子
    剑指offer 字符流中第一个不重复的字符
    leetcode 673. Number of Longest Increasing Subsequence
    leetcode 75. Sort Colors (荷兰三色旗问题)
  • 原文地址:https://www.cnblogs.com/MrSaver/p/9500618.html
Copyright © 2011-2022 走看看