-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathJobs.java
87 lines (50 loc) · 1.49 KB
/
Jobs.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
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
import java.util.*;
public class Jobs {
static class Job implements Comparable<Job> {
String name;
int deadline, profit;
public Job(String n, int d, int p) {
name = n;
deadline = d;
profit = p;
}
@Override
public int compareTo(Job b) {
return b.profit - profit;
}
@Override
public String toString() {
return name;
}
}
public static void printBestOrder(List<Job> jobs) {
Collections.sort(jobs);
int n = jobs.size();
int[] results = new int[n];
boolean[] times = new boolean[n];
for (int i = 0; i < n; i++) {
for (int j = Math.min(n, jobs.get(i).deadline) - 1; j >= 0; j--) {
if (!times[j]) {
results[j] = i;
times[j] = true;
break;
}
}
}
for (int i = 0; i < n; i++) {
if (times[i]) {
System.out.println(jobs.get(results[i]));
}
}
}
public static void main(String[] args) {
List<Job> jobs = new ArrayList<>();
jobs.add(new Job("A", 6, 100));
jobs.add(new Job("B", 2, 100));
jobs.add(new Job("C", 1, 5));
jobs.add(new Job("D", 1, 25));
jobs.add(new Job("E", 3, 90));
jobs.add(new Job("F", 2, 70));
printBestOrder(jobs);
}
}