add: 231014

This commit is contained in:
Eatswap 2023-10-15 00:07:18 +08:00
parent bde1ceb0c2
commit de11fa9dc3
Signed by: Eatswap
GPG Key ID: BE661106A1F3FA0B
2 changed files with 28 additions and 0 deletions

23
cpp/2308/LC231014.cpp Normal file
View File

@ -0,0 +1,23 @@
#include "defs.h"
#include <algorithm>
#include <functional>
#include <unordered_map>
int LC231014::paintWalls(const std::vector<int>& cost, const std::vector<int>& time) {
int n = cost.size();
std::unordered_map<int, int> ans;
std::function<int(int, int)> 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;

View File

@ -31,4 +31,9 @@ public:
static std::string decodeAtIndex(const std::string& s, int k); static std::string decodeAtIndex(const std::string& s, int k);
}; };
class LC231014 {
public:
static int paintWalls(const std::vector<int>&, const std::vector<int>&);
};
#endif //LEETCODE_CPP_DEFS_H #endif //LEETCODE_CPP_DEFS_H