forked from kamyu104/LeetCode-Solutions
-
Notifications
You must be signed in to change notification settings - Fork 0
/
maximum-profit-in-job-scheduling.cpp
48 lines (45 loc) · 1.63 KB
/
maximum-profit-in-job-scheduling.cpp
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
// Time: O(nlogn)
// Space: O(n)
class Solution {
public:
int jobScheduling(vector<int>& startTime, vector<int>& endTime, vector<int>& profit) {
vector<tuple<int, int, int>> jobs;
for (int i = 0; i < startTime.size(); ++i) {
jobs.emplace_back(endTime[i], startTime[i], profit[i]);
}
sort(jobs.begin(), jobs.end());
vector<pair<int, int>> dp = {{0, 0}};
for (const auto& [e, s, p] : jobs) {
const auto& it = prev(upper_bound(dp.cbegin(),
dp.cend(),
make_pair(s + 1, 0)));
if (it->second + p > dp.back().second) {
dp.emplace_back(e, it->second + p);
}
}
return dp.back().second;
}
};
// Time: O(nlogn)
// Space: O(n)
class Solution2 {
public:
int jobScheduling(vector<int>& startTime, vector<int>& endTime, vector<int>& profit) {
vector<tuple<int, int, int>> min_heap;
for (int i = 0; i < startTime.size(); ++i) {
min_heap.emplace_back(startTime[i], endTime[i], profit[i]);
}
make_heap(min_heap.begin(), min_heap.end(), greater<>());
int result = 0;
while (!min_heap.empty()) {
const auto [s, e, p] = min_heap.front();
pop_heap(begin(min_heap), end(min_heap), greater<>()); min_heap.pop_back();
if (s < e) {
min_heap.emplace_back(e, s, result + p); push_heap(begin(min_heap), end(min_heap), greater<>());
} else {
result = max(result, p);
}
}
return result;
}
};