diff --git a/cpp/2308/LC231014.cpp b/cpp/2308/LC231014.cpp new file mode 100644 index 0000000..73bc65a --- /dev/null +++ b/cpp/2308/LC231014.cpp @@ -0,0 +1,23 @@ +#include "defs.h" +#include +#include +#include + +int LC231014::paintWalls(const std::vector& cost, const std::vector& time) { + int n = cost.size(); + + std::unordered_map ans; + std::function dp = [&](int pos, int rem_free) { + if (pos + rem_free >= n) + return 0; + if (pos == n - 1) + return (rem_free + time[pos] < 0) ? 0x70000000 : cost[pos]; + // rem_free < (1 << 21) + int& ret = ans[((rem_free + 505 * 505) << 10) | pos]; + return ret ? ret : ret = std::min(dp(pos + 1, rem_free + time[pos]) + cost[pos], dp(pos + 1, rem_free - 1)); + }; + + return dp(0, 0); +} + +using Solution = LC231014; diff --git a/cpp/2308/defs.h b/cpp/2308/defs.h index 42b50a7..fa640a8 100644 --- a/cpp/2308/defs.h +++ b/cpp/2308/defs.h @@ -31,4 +31,9 @@ public: static std::string decodeAtIndex(const std::string& s, int k); }; +class LC231014 { +public: + static int paintWalls(const std::vector&, const std::vector&); +}; + #endif //LEETCODE_CPP_DEFS_H