-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathWeightedJobScheduling.java
More file actions
76 lines (62 loc) · 2.39 KB
/
WeightedJobScheduling.java
File metadata and controls
76 lines (62 loc) · 2.39 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
75
76
package com.geeksforgeeks.dynamicProgramming;
import com.geeksforgeeks.array.ArrayRotation;
public class WeightedJobScheduling {
public static void main(String[] args) {
int[][] jobDetails = {
{1, 4, 3},
{2, 6, 5},
{4, 7, 2},
{5, 9, 4},
{6, 8, 6},
{7, 10, 8}};
System.out.println("Maximum Profit is " + getMaximumProfit(jobDetails));
}
/**
* JobDetails Array contains each job detail such as {startTime, FinishTime, Profit if job runs}
*
* @param jobDetails
* @return
*/
public static int getMaximumProfit(int[][] jobDetails) {
int[] JS = new int[jobDetails.length]; // This is the Job Scheduling Matrix having profit rates
// Sort JobDetails [{startTime1, finishTime1, profit1},{startTime2, finishTime2, profit2},{startTime3, finishTime3, profit3},]
for (int i = 0; i < jobDetails.length; i++) {
for (int j = 0; j < jobDetails.length - 1; j++) {
if (jobDetails[j][1] > jobDetails[j + 1][1]) {
int[] temp = jobDetails[j];
jobDetails[j] = jobDetails[j + 1];
jobDetails[j + 1] = temp;
}
}
}
// When there is only 1 job in the pipeline so the maximum profit we can get is the profit came as input
for (int i = 0; i < jobDetails.length; i++) {
JS[i] = jobDetails[i][2];
}
//For Every i we will start j from the start (i.e. 0)
int i = 1;
for (int j = 0; j < i && i < JS.length; ) {
//Check both job at i and j can run together or not
if (canJobRunTogether(jobDetails, i, j)) {
// Now let's check these job can actually give us any profit or not
if (JS[j] + jobDetails[i][2] > JS[i]) {
JS[i] = JS[j] + jobDetails[i][2];
}
}
j++;
if (j >= i) {
i++;
j = 0;
}
}
ArrayRotation.printArray(JS);
return JS[JS.length - 1];
}
private static boolean canJobRunTogether(int[][] jobDetails, int i, int j) {
Boolean canJobRunTogether = false;
if (jobDetails[j][1] <= jobDetails[i][0]) {
canJobRunTogether = true;
}
return canJobRunTogether;
}
}