|
| 1 | +/** |
| 2 | + * We have jobs: difficulty[i] is the difficulty of the ith job, and profit[i] |
| 3 | + * is the profit of the ith job. |
| 4 | + * |
| 5 | + * Now we have some workers. worker[i] is the ability of the ith worker, which |
| 6 | + * means that this worker can only complete a job with difficulty at most |
| 7 | + * worker[i]. |
| 8 | + * |
| 9 | + * Every worker can be assigned at most one job, but one job can be completed |
| 10 | + * multiple times. |
| 11 | + * |
| 12 | + * For example, if 3 people attempt the same job that pays $1, then the total |
| 13 | + * profit will be $3. If a worker cannot complete any job, his profit is $0. |
| 14 | + * |
| 15 | + * What is the most profit we can make? |
| 16 | + * |
| 17 | + * Example 1: |
| 18 | + * |
| 19 | + * Input: difficulty = [2,4,6,8,10], profit = [10,20,30,40,50], worker = [4,5,6,7] |
| 20 | + * Output: 100 |
| 21 | + * Explanation: Workers are assigned jobs of difficulty [4,4,6,6] and they get |
| 22 | + * profit of [20,20,30,30] seperately. |
| 23 | + * |
| 24 | + * Notes: |
| 25 | + * 1 <= difficulty.length = profit.length <= 10000 |
| 26 | + * 1 <= worker.length <= 10000 |
| 27 | + * difficulty[i], profit[i], worker[i] are in range [1, 10^5] |
| 28 | + */ |
| 29 | + |
| 30 | +public class MostProfitAssigningWork826 { |
| 31 | + public int maxProfitAssignment(int[] difficulty, int[] profit, int[] worker) { |
| 32 | + int N = difficulty.length; |
| 33 | + Job[] jobs = new Job[N]; |
| 34 | + for (int i=0; i<N; i++) jobs[i] = new Job(difficulty[i], profit[i]); |
| 35 | + Arrays.sort(jobs, (j1, j2) -> Integer.compare(j1.difficulty, j2.difficulty)); |
| 36 | + Arrays.sort(worker); |
| 37 | + int res = 0; |
| 38 | + int max = 0; |
| 39 | + int i = 0; |
| 40 | + for (int cap: worker) { |
| 41 | + while (i < N && jobs[i].difficulty <= cap) { |
| 42 | + max = Math.max(max, jobs[i].profit); |
| 43 | + i++; |
| 44 | + } |
| 45 | + res += max; |
| 46 | + } |
| 47 | + return res; |
| 48 | + } |
| 49 | + |
| 50 | + class Job { |
| 51 | + int difficulty; |
| 52 | + int profit; |
| 53 | + Job (int d, int p) { |
| 54 | + difficulty = d; |
| 55 | + profit = p; |
| 56 | + } |
| 57 | + } |
| 58 | + |
| 59 | +} |
0 commit comments