forked from hrsvrdhn/DP
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathweightedJobSequencing.java
More file actions
42 lines (38 loc) · 954 Bytes
/
weightedJobSequencing.java
File metadata and controls
42 lines (38 loc) · 954 Bytes
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
import java.util.*;
import java.io.*;
class weightedJobSequencing {
public static void solve(int[] profits, int[][] time_interval) {
int[] dp = new int[profits.length];
for(int i=0; i<dp.length; i++)
dp[i] = profits[i];
int max_ind = 0;
for(int i=1; i<dp.length; i++) {
for(int j=0; j<i; j++) {
if(time_interval[j][1] > time_interval[i][0]) continue;
dp[i] = Math.max(dp[i], dp[j] + profits[i]);
}
if(dp[i] > dp[max_ind])
max_ind = i;
}
System.out.println("Max profit is " + dp[max_ind]);
}
public static void main(String args[]) {
int[] profits = {5, 6, 5, 4, 11, 2};
int[][] time_interval = {
{1, 3},
{2, 5},
{4, 6},
{6, 7},
{5, 8},
{7, 9}
};
// Sorting by their endtimes
Arrays.sort(time_interval, new Comparator<int[]>(){
@Override
public int compare(final int[] a, final int[] b) {
return Integer.compare(a[1], b[1]);
}
});
solve(profits, time_interval);
}
}