-
Notifications
You must be signed in to change notification settings - Fork 5
/
Copy pathMostProfitAssigningWork.java
52 lines (49 loc) · 1.51 KB
/
MostProfitAssigningWork.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
/*https://leetcode.com/problems/most-profit-assigning-work/*/
class Job implements Comparable<Job>
{
int difficulty;
int profit;
Job(int d, int p)
{
difficulty = d;
profit = p;
}
@Override
public int compareTo(Job j)
{
return j.profit-this.profit;
}
}
class Solution {
public int maxProfitAssignment(int[] difficulty, int[] profit, int[] worker) {
int totalProfit = 0;
int D = difficulty.length, W = worker.length, i, j;
PriorityQueue<Job> jobExtractor = new PriorityQueue<Job>();
for (i = 0; i < D; ++i)
jobExtractor.add(new Job(difficulty[i],profit[i]));
Job job = null;
job = jobExtractor.poll();
Arrays.sort(worker);
for (i = W-1; i >= 0; --i)
{
while (!jobExtractor.isEmpty() && job.difficulty > worker[i]) job = jobExtractor.poll();
if (job.difficulty > worker[i]) break;
totalProfit += job.profit;
}
return totalProfit;
}
}
class Solution {
public int maxProfitAssignment(int[] difficulty, int[] profit, int[] worker) {
int max = 0, i, D = difficulty.length;
for (int d : difficulty) if (d > max) max = d;
int[] dp = new int[max+1];
for (i = 0; i < D; ++i)
dp[difficulty[i]] = Math.max(dp[difficulty[i]], profit[i]);
for (i = 1; i < max+1; ++i)
dp[i] = Math.max(dp[i], dp[i-1]);
int sum = 0;
for (int w : worker) if (w > max) sum += dp[max]; else sum += dp[w];
return sum;
}
}