-
Notifications
You must be signed in to change notification settings - Fork 0
/
job_scheduling_profit.cc
30 lines (26 loc) · 1.07 KB
/
job_scheduling_profit.cc
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
typedef std::pair<int, int> Pair;
class Solution {
public:
int jobScheduling(vector<int>& startTime, vector<int>& endTime, vector<int>& profit) {
std::map<int, int> OPT;
std::unordered_map<int, vector<Pair>> jobs;
int max_profit = 0;
// Initialize DP conditions
for (int i = 0; i < startTime.size(); i++) {
OPT[startTime[i]] = 0;
jobs[startTime[i]].push_back({endTime[i], profit[i]});
}
// Traverse the tree from right to left
for (auto it = OPT.rbegin(); it != OPT.rend(); it++) {
// Get associaited job
for (auto job : jobs[it->first]) {
auto i = OPT.lower_bound(job.first);
// Make a decision. Max of current profit or (profit from start time + current profit)
max_profit = std::max(max_profit, (i == OPT.end() ? 0 : i->second) + job.second);
}
// Update profit for that start time
it->second = max_profit;
}
return max_profit;
}
};