LeetCode 826 Most Profit Assigning Work

LeetCode 826 Most Profit Assigning Work

传送门

题目分析

We have jobs: difficulty[i] is the difficulty of the ith job, and profit[i] is the profit of the ith job.

Now we have some workers. worker[i] is the ability of the ith worker, which means that this worker can only complete a job with difficulty at most worker[i].

Every worker can be assigned at most one job, but one job can be completed multiple times.

For example, if 3 people attempt the same job that pays 1,thenthetotalprofitwillbe 1 , t h e n t h e t o t a l p r o f i t w i l l b e 3. If a worker cannot complete any job, his profit is $0.

What is the most profit we can make?

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.

Notes:

  • 1 <= difficulty.length = profit.length <= 10000
  • 1 <= worker.length <= 10000
  • difficulty[i], profit[i], worker[i] are in range [1, 10^5]

给定工作难度,工资,工人的能力,三个数组,可能每份工作可以重复使用,计算最大利润。

思考

首先数据量很大,直接暴力写是不太可能了,可以先对难度和利润进行排序,这样worker的遍历时就可以及时停止遍历了,还有就是使用HashMap记录已经有了的worker数据进行重用,因为能力相同的工人可以得到的最大利润也是相同的。

代码实现

import java.util.HashMap;

class Solution {
    public int maxProfitAssignment(int[] difficulty, int[] profit, int[] worker) {
        if (difficulty.length == 0 || worker.length == 0) {
            return 0;
        }

        int result = 0;
        // 变形快排,将diffifulty和profit绑在一起排序
        quickSort(difficulty, profit, 0, difficulty.length - 1);
        HashMap map = new HashMap<>();
        for (int abi : worker) {
            // find work for person
            if (map.containsKey(abi)) {
                result += map.get(abi);
                continue;
            }
            int money = 0;
            for (int i = 0; i < difficulty.length; ++i) {
                if (difficulty[i] <= abi) {
                    money = Math.max(money, profit[i]);
                } else {
                    break;
                }
            }
            result += money;
            map.put(abi, money);
        }

        return result;
    }
    // 快排变形,以diffi为主键,对pro和diffi同时排序
    void quickSort(int[] diffi, int[] pro, int start, int end) {
        if (start >= end) {
            return;
        }

        int s = start, e = end;
        int dmid = diffi[s];
        int pmid = pro[s];
        while (s < e) {
            while (s < e && diffi[e] >= dmid) {
                --e;
            }
            swap(diffi, s, e);
            swap(pro, s, e);
            while (s < e && diffi[s] <= dmid) {
                ++s;
            }
            swap(diffi, s, e);
            swap(pro, s, e);
        }

        diffi[s] = dmid;
        pro[s] = pmid;
        quickSort(diffi, pro, start, s - 1);
        quickSort(diffi, pro, s + 1, end);

    }

    void swap(int[] arr, int a, int b) {
        int t = arr[a];
        arr[a] = arr[b];
        arr[b] = t;
    }
}

感想

代码时间复杂度太惨了,不知道可以怎么改进啊?

你可能感兴趣的:(Java,OJ,LeetCode)