-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy path1235.maximum-profit-in-job-scheduling.java
80 lines (65 loc) · 2.57 KB
/
1235.maximum-profit-in-job-scheduling.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
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
/*
* @lc app=leetcode id=1235 lang=java
*
* [1235] Maximum Profit in Job Scheduling
*/
// @lc code=start
// Top Down DP + Binary Search
class Solution {
// maximum number of jobs are 50000
int[] memo = new int[50001];
private int findNextJob(int[] startTime, int lastEndingTime) {
int start = 0, end = startTime.length - 1, nextIndex = startTime.length;
while (start <= end) {
int mid = (start + end) / 2;
if (startTime[mid] >= lastEndingTime) {
nextIndex = mid;
end = mid - 1;
} else {
start = mid + 1;
}
}
return nextIndex;
}
private int findMaxProfit(List<List<Integer>> jobs, int[] startTime, int n, int position) {
// 0 profit if we have already iterated over all the jobs
if (position == n) {
return 0;
}
// return result directly if it's calculated
if (memo[position] != -1) {
return memo[position];
}
// nextIndex is the index of next non-conflicting job
int nextIndex = findNextJob(startTime, jobs.get(position).get(1));
// find the maximum profit of our two options: skipping or scheduling the
// current job
int maxProfit = Math.max(findMaxProfit(jobs, startTime, n, position + 1),
jobs.get(position).get(2) + findMaxProfit(jobs, startTime, n, nextIndex));
// return maximum profit and also store it for future reference (memoization)
return memo[position] = maxProfit;
}
public int jobScheduling(int[] startTime, int[] endTime, int[] profit) {
List<List<Integer>> jobs = new ArrayList<>();
// marking all values to -1 so that we can differentiate
// if we have already calculated the answer or not
Arrays.fill(memo, -1);
// storing job's details into one list
// this will help in sorting the jobs while maintaining the other parameters
int length = profit.length;
for (int i = 0; i < length; i++) {
ArrayList<Integer> currJob = new ArrayList<>();
currJob.add(startTime[i]);
currJob.add(endTime[i]);
currJob.add(profit[i]);
jobs.add(currJob);
}
jobs.sort(Comparator.comparingInt(a -> a.get(0)));
// binary search will be used in startTime so store it as separate list
for (int i = 0; i < length; i++) {
startTime[i] = jobs.get(i).get(0);
}
return findMaxProfit(jobs, startTime, length, 0);
}
}
// @lc code=end