-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathJobSequencing.java
More file actions
74 lines (58 loc) · 1.72 KB
/
JobSequencing.java
File metadata and controls
74 lines (58 loc) · 1.72 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
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
import java.util.*;
class Job{
char id;
int deadline, profit;
Job(char x, int y, int z){
this.id = x;
this.deadline = y;
this.profit = z;
}
}
class Solve{
int[] JobScheduling(Job[] arr, int n){
Arrays.sort(arr, (a,b) -> (b.profit - a.profit));
int maxi=0;
for (int i = 0; i < n; i++) {
if(arr[i].deadline > maxi){
maxi=arr[i].deadline;
}
}
int[] result = new int[maxi+1];
for (int i = 1; i <= maxi; i++) {
result[i] = -1;
}
int countJobs=0, jobProfit=0;
for (int i = 0; i < n; i++) {
for (int j = arr[i].deadline; j > 0; j--) {
if(result[j] == -1){
result[j]=i;
countJobs++;
jobProfit += arr[i].profit;
break;
}
}
}
int[] ans = new int[2];
ans[0] = countJobs;
ans[1] = jobProfit;
return ans;
}
}
public class JobSequencing {
public static void main(String[] args){
// Job[] arr = new Job[4];
// arr[0] = new Job('a', 4, 20);
// arr[1] = new Job('b', 1, 10);
// arr[2] = new Job('c', 2, 40);
// arr[3] = new Job('d', 2, 30);
Job[] arr = new Job[5];
arr[0] = new Job('a', 2, 100);
arr[1] = new Job('b', 1, 19);
arr[2] = new Job('c', 2, 27);
arr[3] = new Job('d', 1, 25);
arr[4] = new Job('e', 3, 15);
Solve ob = new Solve();
int[] res = ob.JobScheduling(arr, 5);
System.out.println(res[0] + " " + res[1]);
}
}