-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathWeightedJobScheduling.java
More file actions
44 lines (35 loc) · 1.06 KB
/
WeightedJobScheduling.java
File metadata and controls
44 lines (35 loc) · 1.06 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
package Greedy;
import java.util.Arrays;
import java.util.Comparator;
public class WeightedJobScheduling {
public static int[] JobScheduling(Job[] arr, int n) {
Arrays.sort(arr, new Comparator<>() {
@Override
public int compare(Job j1, Job j2) {
return j1.profit == j2.profit ?
Integer.compare(j1.deadline, j2.deadline) :
Integer.compare(j2.profit, j1.profit);
}
});
int jobs = 0, max = 0;
boolean[] isDone = new boolean[n];
for (Job job : arr) {
for (int i = job.deadline - 1; i >= 0; i--) {
if (isDone[i]) continue;
isDone[i] = true;
jobs++;
max += job.profit;
break;
}
}
return new int[]{jobs, max};
}
public static class Job {
int id, profit, deadline;
Job(int x, int y, int z) {
this.id = x;
this.deadline = y;
this.profit = z;
}
}
}