Ai
1 Star 2 Fork 5

LilithSangreal/LeetCode-Solutions

加入 Gitee
与超过 1200万 开发者一起发现、参与优秀开源项目,私有仓库也完全免费 :)
免费加入
文件
克隆/下载
minimum-cost-for-tickets.cpp 847 Bytes
一键复制 编辑 原始数据 按行查看 历史
kamyu 提交于 2019-01-29 10:07 +08:00 . Update minimum-cost-for-tickets.cpp
// Time: O(n)
// space: O(1)
class Solution {
public:
int mincostTickets(vector<int>& days, vector<int>& costs) {
static vector<int> durations{1, 7, 30};
const int W = durations.back();
vector<int> dp(W, numeric_limits<int>::max());
dp[0] = 0;
vector<int> last_buy_days{0, 0, 0};
for (int i = 1; i < days.size() + 1; ++i) {
dp[i % W] = numeric_limits<int>::max();
for (int j = 0; j < durations.size(); ++j) {
while (i - 1 < days.size() &&
days[i - 1] > days[last_buy_days[j]] + durations[j] - 1) {
++last_buy_days[j]; // Time: O(n)
}
dp[i % W] = min(dp[i % W], dp[last_buy_days[j] % W] + costs[j]);
}
}
return dp[days.size() % W];
}
};
Loading...
马建仓 AI 助手
尝试更多
代码解读
代码找茬
代码优化
1
https://gitee.com/LilithSangreal/LeetCode-Solutions.git
git@gitee.com:LilithSangreal/LeetCode-Solutions.git
LilithSangreal
LeetCode-Solutions
LeetCode-Solutions
master

搜索帮助