From 542023aa3884499b2057c2d36ba34e67618f129a Mon Sep 17 00:00:00 2001 From: Eatswap Date: Sat, 22 Oct 2022 01:54:55 +0800 Subject: [PATCH] add: 221021 --- cpp/2210/221022-CN.cpp | 61 +++++++++++++++++++++++++++++++++++++++++ cpp/2210/CMakeLists.txt | 2 +- 2 files changed, 62 insertions(+), 1 deletion(-) create mode 100644 cpp/2210/221022-CN.cpp diff --git a/cpp/2210/221022-CN.cpp b/cpp/2210/221022-CN.cpp new file mode 100644 index 0000000..b472ffb --- /dev/null +++ b/cpp/2210/221022-CN.cpp @@ -0,0 +1,61 @@ +#include +#include +#include +#include +#include +#include +#include + +/** + * 1235. Maximum Profit in Job Scheduling + * + * We have n jobs, where every job is scheduled to be done from startTime[i] to endTime[i], obtaining a profit of profit[i]. + * You're given the startTime, endTime and profit arrays, return the maximum profit you can take such that there are no two jobs in the subset with overlapping time range. + * If you choose a job that ends at time X you will be able to start another job that starts at time X. + */ + +class Solution { +public: + static int jobScheduling(const std::vector& st, const std::vector& et, const std::vector& p) { + std::vector> info; + int maxTS; + { + std::set s(st.begin(), st.end()); + s.insert(et.begin(), et.end()); + maxTS = s.size(); + int i = 0; + std::unordered_map tm; + for (const int t : s) + tm[t] = i++; + info.reserve(st.size()); + for (int j = st.size(); j--; ) + info.emplace_back(tm[st[j]], tm[et[j]], p[j]); + } + std::sort(info.begin(), info.end()); + + std::vector psa(maxTS, -1); + std::function dp = [&](int sp) { + if (psa[sp] >= 0) + return psa[sp]; + auto pos = std::lower_bound(info.begin(), info.end(), std::make_tuple(sp, 0, 0)); + if (pos == info.end()) + return psa[sp] = 0; + int ans = 0, lim = 0x7FFFFFFF; + for (auto it = pos; it != info.end() && std::get<0>(*it) < lim; ++it) { + const auto [_, ets, cp] = *it; + ans = std::max(ans, dp(ets) + cp); + lim = std::min(lim, ets); + } + return psa[sp] = ans; + }; + + return dp(0); + } +}; + +int main() { + std::cout << Solution::jobScheduling({1,2,3,3}, {3,4,5,6}, {50,10,40,70}) << "\n"; + std::cout << Solution::jobScheduling({1,2,3,4,6}, {3,4,10,6,9}, {20,20,100,70,60}) << "\n"; + std::cout << Solution::jobScheduling({1,1,1}, {2,3,4}, {5,6,4}) << "\n"; + return 0; +} diff --git a/cpp/2210/CMakeLists.txt b/cpp/2210/CMakeLists.txt index b64967f..45b05cf 100644 --- a/cpp/2210/CMakeLists.txt +++ b/cpp/2210/CMakeLists.txt @@ -3,4 +3,4 @@ PROJECT(2210) SET(CMAKE_CXX_STANDARD 23) -ADD_EXECUTABLE(2210 221021.cpp) +ADD_EXECUTABLE(2210 221022-CN.cpp)