Skip to content

Commit 6896872

Browse files
committed
MostProfitAssigningWork826
1 parent 0f36a7b commit 6896872

File tree

2 files changed

+62
-2
lines changed

2 files changed

+62
-2
lines changed

README.md

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -44,11 +44,11 @@
4444
| [Graph](https://github.com/fluency03/leetcode-java/blob/master/src/graph) |
4545

4646

47-
# Total: 381
47+
# Total: 382
4848

4949
| Easy | Medium | Hard | - |
5050
|:-------:|:-------:|:----:|:-:|
51-
| 102 | 209 | 66 | 4 |
51+
| 102 | 210 | 66 | 4 |
5252

5353

5454
| Question | Solution | Difficulty |
@@ -422,6 +422,7 @@
422422
| [777. Swap Adjacent in LR String](https://leetcode.com/problems/swap-adjacent-in-lr-string/) | [Solution](https://github.com/fluency03/leetcode-java/blob/master/src/SwapAdjacentInLRString777.java) | Medium |
423423
| [783. Minimum Distance Between BST Nodes](https://leetcode.com/problems/minimum-distance-between-bst-nodes/) | [Solution](https://github.com/fluency03/leetcode-java/blob/master/src/MinimumDistanceBetweenBSTNodes783.java) | Easy |
424424
| [819. Most Common Word](https://leetcode.com/problems/most-common-word/) | [Solution](https://github.com/fluency03/leetcode-java/blob/master/src/MostCommonWord819.java) | Easy |
425+
| [826. Most Profit Assigning Work](https://leetcode.com/problems/most-profit-assigning-work/) | [Solution](https://github.com/fluency03/leetcode-java/blob/master/src/MostProfitAssigningWork826.java) | Medium |
425426
| [841. Keys and Rooms](https://leetcode.com/problems/keys-and-rooms/) | [Solution](https://github.com/fluency03/leetcode-java/blob/master/src/KeysAndRooms841.java) | Medium |
426427
| [844. Backspace String Compare](https://leetcode.com/problems/backspace-string-compare/) | [Solution](https://github.com/fluency03/leetcode-java/blob/master/src/BackspaceStringCompare844.java) | Easy |
427428
| [845. Longest Mountain in Array](https://leetcode.com/problems/longest-mountain-in-array/) | [Solution](https://github.com/fluency03/leetcode-java/blob/master/src/LongestMountainInArray845.java) | Medium |

src/MostProfitAssigningWork826.java

Lines changed: 59 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,59 @@
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

Comments
 (0)