-
Notifications
You must be signed in to change notification settings - Fork 1
/
WeightedSchDp.java
48 lines (42 loc) 路 1.29 KB
/
WeightedSchDp.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
import java.util.ArrayList;
import java.util.Arrays;
class Scheduling {
public static void main(String[] args) {
// start, finish, profit
int[][] Job = {{3, 10, 20}, {1, 2, 50},{2, 100, 200}, {6, 19, 100} };
InsertionSort(Job); //SORT ACCORDING TO FINISH TIME
System.out.println("The max profit is : "+checkProfit(Job));
}
public static int checkProfit(int[][] Job){
int n = Job.length;
int[] store = new int[n];
store[0]=Job[0][2];
for (int i = 1; i < n; i++) {
int current = Job[i][2];
int out = findPrevJob(Job,i);
if(out!=-1){
current+=store[out];
}
store[i] = Math.max(current,store[i-1]);
}
return store[store.length-1];
}
public static int findPrevJob(int[][] Job,int i){
for (int j = i-1; j>=0; j--) {
if (Job[j][1]<= Job[i][0]) return j;
}
return -1;
}
public static void InsertionSort(int[][] job){
int[] key,key1,key2;
for (int i = 0; i < job.length; i++) {
key = job[i];
int j=i-1;
while (j>=0 && job[j][1]>key[1]){
job[j+1]=job[j];
j--;
}
job[j+1]=key;
}
}
}