-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathJobScheduling.java
More file actions
47 lines (41 loc) · 1.16 KB
/
JobScheduling.java
File metadata and controls
47 lines (41 loc) · 1.16 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
package Codes.GreddyAlgorithm;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collections;
class Job{
char id;
int deadline, profit;
public Job(char c,int d, int p){
id=c;
deadline=d;
profit=p;
}
}
public class JobScheduling {
static void jobSchdule(ArrayList<Job> arr, int d){
Collections.sort(arr, (a,b)->b.profit-a.profit);
boolean res[]= new boolean[d];
char jobs[]= new char[d];
for(int i=0;i< arr.size();i++){
for(int j=Math.min(d-1, arr.get(i).deadline-1);j>=0;j--){
if(!res[j]){
res[j]=true;
jobs[j]=arr.get(i).id;
break;
}
}
}
for(int i=0;i<jobs.length;i++)
System.out.print(jobs[i]+" " );
}
public static void main(String[] args) {
ArrayList <Job> arr= new ArrayList<>();
arr.add(new Job('a', 2, 100));
arr.add(new Job('b', 1, 19));
arr.add(new Job('c', 2, 27));
arr.add(new Job('d', 1, 25));
arr.add(new Job('e', 3, 15));
int d=3;
jobSchdule(arr,d);
}
}