-
Notifications
You must be signed in to change notification settings - Fork 0
/
JobSequencing.java
36 lines (34 loc) · 999 Bytes
/
JobSequencing.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
import java.util.*;
public class Solution
{
public static int jobScheduling(int[][] jobs)
{
int profit = 0;
int maxDeadline = 0;
Jobs[] job = new Jobs[jobs.length];
for(int i=0;i<job.length;i++){
job[i] = new Jobs(jobs[i][0],jobs[i][1]);
maxDeadline = Math.max(maxDeadline,jobs[i][0]);
}
boolean[] task = new boolean[maxDeadline+1];
Arrays.sort(job,(Jobs a,Jobs b)->b.profit-a.profit);
for(int i=0;i<job.length;i++){
for(int j=job[i].deadline;j>0;j--){
if(!task[j]){
profit += job[i].profit;
task[j] = true;
break;
}
}
}
return profit;
}
public static class Jobs{
int deadline;
int profit;
Jobs(int d,int p){
this.deadline = d;
this.profit = p;
}
}
}