Q826 安排工作以达到最大收益

有一些工作: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]
class Solution {
    public int maxProfitAssignment(int[] difficulty, int[] profit, int[] worker) {
        int result = 0;
        HashMap<Integer, Integer> map = new HashMap<>();

        for (int i : worker) {
            int t = 0;
            if (map.containsKey(i)) {
                t = map.get(i);
            } else {
                for (int j = 0; j < difficulty.length; j++) {
                    if (difficulty[j] <= i) {
                        t = profit[j] > t ? profit[j] : t;
                    }
                }
            }
            result += t;
        }

        return result;
    }
}

猜你喜欢

转载自www.cnblogs.com/WeichengDDD/p/10853349.html