diff --git a/cpp/2304/230420-CN.cpp b/cpp/2304/230420-CN.cpp new file mode 100644 index 0000000..6df1b3c --- /dev/null +++ b/cpp/2304/230420-CN.cpp @@ -0,0 +1,41 @@ +#include +#include +#include +#include +#include + +/** + * 1187. Make Array Strictly Increasing + * + * Given two integer arrays arr1 and arr2, return the minimum number of operations (possibly zero) needed to make arr1 strictly increasing. + * In one operation, you can choose two indices 0 <= i < arr1.length and 0 <= j < arr2.length and do the assignment arr1[i] = arr2[j]. + * If there is no way to make arr1 strictly increasing, return -1. + */ + +class Solution { +public: + static int makeArrayIncreasing(const std::vector&, std::vector); +}; + +int Solution::makeArrayIncreasing(const std::vector& p, std::vector q) { + std::sort(q.begin(), q.end()); + std::unordered_map ans[2048]; + std::function dp = [&](int pos, int prev) { + if (pos >= p.size()) + return 0; + if (ans[pos].count(prev)) + return ans[pos][prev]; + int ret = 0x7FFFFFFE; + if (auto it = std::upper_bound(q.begin(), q.end(), prev); it != q.end()) + ret = std::min(ret, dp(pos + 1, *it) + 1); + if (p[pos] > prev) + ret = std::min(ret, dp(pos + 1, p[pos])); + return ans[pos][prev] = ret; + }; + return dp(0, -1) > 99999 ? -1 : dp(0, -1); +} + +int main() { + std::cout << Solution::makeArrayIncreasing({1,5,3,6,7},{6,3,3,1}); + return 0; +} diff --git a/cpp/2304/CMakeLists.txt b/cpp/2304/CMakeLists.txt index 04b7d85..977590a 100644 --- a/cpp/2304/CMakeLists.txt +++ b/cpp/2304/CMakeLists.txt @@ -4,4 +4,4 @@ PROJECT(2304) SET(CMAKE_CXX_STANDARD 23) SET(CMAKE_EXPORT_COMPILE_COMMANDS true) -ADD_EXECUTABLE(2304 230419-CN.cpp) +ADD_EXECUTABLE(2304 230420-CN.cpp)