-
Notifications
You must be signed in to change notification settings - Fork 0
/
1235. Maximum Profit in Job Scheduling
49 lines (41 loc) · 1.45 KB
/
1235. Maximum Profit in Job Scheduling
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
struct Job {
int startTime;
int endTime;
int profit;
Job(int startTime, int endTime, int profit)
: startTime(startTime), endTime(endTime), profit(profit) {}
};
class Solution {
public:
int jobScheduling(vector<int>& startTime, vector<int>& endTime,
vector<int>& profit) {
const int n = startTime.size();
vector<int> mem(n + 1);
vector<Job> jobs;
for (int i = 0; i < n; ++i)
jobs.emplace_back(startTime[i], endTime[i], profit[i]);
ranges::sort(jobs, [](const Job& a, const Job& b) {
return a.startTime < b.startTime;
});
// Will use binary search to find the first available start time.
for (int i = 0; i < n; ++i)
startTime[i] = jobs[i].startTime;
return jobScheduling(jobs, startTime, 0, mem);
}
private:
// Returns the maximum profit to schedule jobs[i..n).
int jobScheduling(const vector<Job>& jobs, const vector<int>& startTime,
int i, vector<int>& mem) {
if (i == jobs.size())
return 0;
if (mem[i] > 0)
return mem[i];
const int j = firstGreaterEqual(startTime, i + 1, jobs[i].endTime);
const int pick = jobs[i].profit + jobScheduling(jobs, startTime, j, mem);
const int skip = jobScheduling(jobs, startTime, i + 1, mem);
return mem[i] = max(pick, skip);
}
int firstGreaterEqual(const vector<int>& A, int startFrom, int target) {
return lower_bound(A.begin() + startFrom, A.end(), target) - A.begin();
}
};