-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathJobSequencing.java
58 lines (55 loc) · 1.2 KB
/
JobSequencing.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
package Unit_1;
import java.util.*;
class Job
{
String id;
int profit, deadline;
Job(String i, int p, int dl)
{
id = i;
profit = p;
deadline = dl;
}
}
public class JobSequencing {
public static void main(String args[])
{
Scanner sc = new Scanner(System.in);
String id;
int dl, p, n, profit = 0;
System.out.print("Enter the number of jobS : ");
n = sc.nextInt();
int max = Integer.MIN_VALUE;
ArrayList<Job> a = new ArrayList<>();
for(int i = 0; i < n; i++)
{
System.out.print("Enter id, deadline and profit :");
id = sc.next();
dl = sc.nextInt();
p = sc.nextInt();
max = Math.max(max, dl);
a.add(new Job(id, p, dl));
}
Collections.sort(a, (n1, n2) -> n2.profit - n1.profit);
boolean result[] = new boolean[max];
String job[] = new String[max];
for(int i = 0; i < n; i++)
{
for(int j = Math.min(max - 1, (a.get(i).deadline - 1)); j >= 0; j--)
{
if(result[j] == false)
{
result[j] = true;
job[j] = a.get(i).id;
profit += a.get(i).profit;
break;
}
}
}
System.out.print("Job sequencing result : ");
for(String s : job)
System.out.print(s + " ");
System.out.println("\nProfit earned : " + profit);
sc.close();
}
}