-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy path1235-maximum-profit-in-job-scheduling.cpp
68 lines (58 loc) · 2.27 KB
/
1235-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
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
/* TLE
class Solution {
public:
int jobScheduling(vector<int>& st, vector<int>& et, vector<int>& p) {
vector<array<int, 3>> ranges;
for (int i = 0; i < st.size(); i++) ranges.push_back({st[i], et[i], p[i]});
sort(ranges.begin(), ranges.end(), [](auto l, auto r) {
return l[0] < r[0];
});
unordered_map<string, int> store;
auto gen_key = [](auto l, auto ... r) {
return (to_string(l) + ... + ("-" + to_string(r)));
};
function<int(int, int)> go = [&](auto i, auto till) {
if (i == ranges.size()) return 0;
auto key = gen_key(i, till);
if (store.count(key)) return store[key];
auto no_take = go(i + 1, till), take = 0;
if (ranges[i][0] >= till)
take = ranges[i][2] + go(i + 1, ranges[i][1]);
return store[key] = max(no_take, take);
};
return go(0, 0);
}
};
*/
class Solution {
public:
// int jobScheduling(vector<int>& s, vector<int>& e, vector<int>& p) {
// unordered_map<int, vector<pair<int, int>>> um;
// for (auto i = 0; i < p.size(); i++) um[e[i]].emplace_back(s[i], p[i]);
// auto n = *max_element(e.begin(), e.end());
// unordered_map<int, int> dp; dp[0] = 0;
// for (int i = 1; i <= n; i++) {
// if (!um.count(i)) { dp[i] = dp[i - 1]; continue; }
// for (auto [l, p]: um[i]) {
// auto include = dp[l] + p, exclude = dp[i-1];
// dp[i] = max({dp[i], include, exclude});
// }
// }
// return dp[n];
// }
int jobScheduling(vector<int>& startTime,
vector<int>& endTime, vector<int>& profit) {
int n = startTime.size();
vector<vector<int>> jobs;
for (int i = 0; i < n; ++i)
jobs.push_back({endTime[i], startTime[i], profit[i]});
sort(jobs.begin(), jobs.end());
map<int, int> dp = {{0, 0}};
for (auto& job : jobs) {
int cur = prev(dp.upper_bound(job[1]))->second + job[2];
if (cur > dp.rbegin()->second)
dp[job[0]] = cur;
}
return dp.rbegin()->second;
}
};