-
Notifications
You must be signed in to change notification settings - Fork 0
/
job_seq.java
63 lines (53 loc) · 1.52 KB
/
job_seq.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
import java.util.ArrayList;
class Job{
int id;
int profit;
int deadline;
public Job(int id,int profit,int deadline){
this.id=id;
this.profit=profit;
this.deadline=deadline;
}
}
public class job_seq {
public static void JS(int n ,Job[] a,int t){
for(int i=0;i<n;i++){
for(int k=i+1;k<n;k++){
if(a[i].profit<a[k].profit){
Job temp=a[i];
a[i]=a[k];
a[k]=temp;
}
}
}
boolean[] result=new boolean[t];
int[] job= new int[t];
for(int i=0;i<n;i++){
for(int j=Math.min(t-1,a[i].deadline-1);j>=0;j--){
if (result[j] == false) {
result[j] = true;
job[j] = a[i].id;
break;
}
}
}
for (int jb : job)
System.out.print((jb+1)+ " ");
System.out.println();
}
public static void main(String args[]) {
Job[] jobs=new Job[6];
int n=6;
// Function call
System.out.println("Following is maximum profit sequence of Jobs: ");
jobs[0] = new Job(0,20,3);
jobs[1] = new Job(1,15,1);
jobs[2] = new Job(2,10,1);
jobs[3] = new Job(3,7,3);
jobs[4] = new Job(4,5,1);
jobs[5] = new Job(5,3,3);
int t=3;
// Calling function
JS(n, jobs, t);
}
}