forked from Sunchit/Coding-Decoded
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathMaximumProfitInJobScheduling.java
36 lines (29 loc) · 1.06 KB
/
MaximumProfitInJobScheduling.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
class Solution {
private class Job{
private int startTime;
private int endTime;
private int profit;
public Job(int sT, int eT, int p){
this.startTime = sT;
this.endTime = eT;
this.profit = p;
}
}
public int jobScheduling(int[] startTime, int[] endTime, int[] profit) {
List<Job> jobs = new ArrayList<>();
for (int i=0; i<startTime.length; i++) {
jobs.add(new Job(startTime[i],endTime[i], profit[i]));
}
Collections.sort(jobs, (a,b) -> ( a.endTime - b.endTime));
// Key => Time, Value => profitTillTime
TreeMap<Integer, Integer> map = new TreeMap<>();
int ans = 0;
for (Job currJob : jobs) {
Integer entryTillStartTime = map.floorKey(currJob.startTime);
int maxProfitTillStartTime = entryTillStartTime ==null?0:map.get(entryTillStartTime);
ans = Math.max(ans, maxProfitTillStartTime+currJob.profit);
map.put(currJob.endTime, ans);
}
return ans;
}
}