-
-
Notifications
You must be signed in to change notification settings - Fork 291
Expand file tree
/
Copy path826.java
More file actions
66 lines (58 loc) · 2.15 KB
/
826.java
File metadata and controls
66 lines (58 loc) · 2.15 KB
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
53
54
55
56
57
58
59
60
61
62
63
64
65
66
__________________________________________________________________________________________________
sample 3 ms submission
class Solution {
public int maxProfitAssignment(int[] difficulty, int[] profit, int[] worker) {
int ret = 0, maxIdx = 0;
for(int idx : difficulty)
maxIdx = Math.max(maxIdx, idx);
int[] profits = new int[maxIdx+1];
for(int i=0;i<difficulty.length;i++){
profits[difficulty[i]] = Math.max(profit[i], profits[difficulty[i]]);
}
int tmpMax = 0;
for(int i=1;i< profits.length;i++){
tmpMax = Math.max(tmpMax, profits[i]);
profits[i] = tmpMax;
}
for(int idx : worker){
if(idx > maxIdx) ret+=profits[maxIdx];
else ret+=profits[idx];
}
return ret;
}
}
__________________________________________________________________________________________________
sample 38688 kb submission
class Solution {
static class Task {
int diff;
int profit;
int id;
public Task(int id, int d, int p) {
this.id = id;
this.diff = d;
this.profit = p;
}
}
public int maxProfitAssignment(int[] difficulty, int[] profit, int[] worker) {
if(difficulty.length == 0 || worker.length == 0) return 0;
List<Task> list = new ArrayList<>();
for(int i = 0; i < difficulty.length; i ++) {
Task t = new Task(i, difficulty[i], profit[i]);
list.add(t);
}
Collections.sort(list, (a, b) -> {
if(a.profit == b.profit) return a.diff - b.diff;
return b.profit - a.profit;
});
Arrays.sort(worker);
int j = 0, res = 0;
for(int i = worker.length - 1; i >= 0; i --) {
while(j < list.size() && list.get(j).diff > worker[i]) j ++;
if(j == list.size()) break;
res += list.get(j).profit;
}
return res;
}
}
__________________________________________________________________________________________________