-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathWeightedJobScheduling.java
More file actions
58 lines (57 loc) · 1.11 KB
/
WeightedJobScheduling.java
File metadata and controls
58 lines (57 loc) · 1.11 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
import java.util.*;
class WeightedJobScheduling {
static class Job{
int start,end,weight;
Job(int s,int e,int w){
start=s;
end=e;
weight=w;
}
}
public static int JobScheduling(Job[] J,int n) {
Arrays.sort(J, new Comparator<Job>(){
@Override
public int compare(Job o1, Job o2) {
if(o1.end<=o2.end) {
return -1;
}
else {
return 1;
}
}
});
int[] dp=new int[n];
int temp=-1;
dp[0]=J[0].weight;
for(int i=1;i<n;i++) {
temp=-1;
for(int j=0;j<i;j++) {
if(J[j].end<=J[i].start) {
temp=j;
}
}
if(temp==-1) {
dp[i]=Math.max(J[i].weight, dp[i-1]);
}
else {
dp[i]=Math.max(dp[i-1], dp[temp]+J[i].weight);
}
}
//System.out.println(Arrays.toString(dp));
return dp[n-1];
}
public static void main(String[] args) {
int n,s,e,w;
Scanner sc=new Scanner(System.in);
n=sc.nextInt();
Job[] J=new Job[n];
for(int i=0;i<n;i++) {
s=sc.nextInt();
e=sc.nextInt();
w=sc.nextInt();
J[i]=new Job(s,e,w);
}
System.out.println(JobScheduling(J,n));
sc.close();
}
}