forked from striver79/SDESheet
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathjobSequencingJava
44 lines (35 loc) · 1.12 KB
/
jobSequencingJava
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
class solve{
// return an array of size 2 having the 0th element equal to the count
// and 1st element equal to the maximum profit
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--) {
// Free slot found
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;
}
}