-
Notifications
You must be signed in to change notification settings - Fork 0
/
a_150_Job_Sequence.java
49 lines (36 loc) · 1.15 KB
/
a_150_Job_Sequence.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
import java.util.* ;
public class a_150_Job_Sequence {
static class Job{
int id ;
int deadline ;
int profit ;
public Job (int i, int d, int p){
id = i ;
deadline = d ;
profit = p ;
}
}
public static void main(String[] args) {
int jobInfo[][] = { {4, 20 }, {1, 10 }, {1, 40}, {1, 30} } ;
ArrayList<Job> jobs = new ArrayList<>() ;
for(int i=0; i<jobInfo.length; i++){
jobs.add( new Job(i, jobInfo[i][0], jobInfo[i][1] ) ) ;
}
// descending order of profit
Collections.sort(jobs, (obj1, obj2) -> obj2.profit - obj1.profit ) ;
ArrayList<Integer> seq = new ArrayList<>() ;
int time = 0 ;
for(int i=0; i<jobs.size() ; i++ ) {
Job curr = jobs.get(i) ;
if(curr.deadline > time ) {
seq.add(curr.id) ;
time ++ ;
}
}
// Print seq
for(int i=0; i<seq.size() ; i++) {
System.out.print(seq.get(i) + " ");
}
System.out.println();
}
}