1 Star 0 Fork 0

wd6/LeetCode-1

加入 Gitee
与超过 1200万 开发者一起发现、参与优秀开源项目,私有仓库也完全免费 :)
免费加入
文件
克隆/下载
coin-path.cpp 1.01 KB
一键复制 编辑 原始数据 按行查看 历史
kamyu 提交于 8年前 . Create coin-path.cpp
// Time: O(n * B)
// Space: O(n)
class Solution {
public:
vector<int> cheapestJump(vector<int>& A, int B) {
vector<int> result;
if (A.empty() || A.back() == -1) {
return result;
}
const int n = A.size();
vector<int> dp(n, numeric_limits<int>::max()), next(n, -1);
dp[n - 1] = A[n - 1];
for (int i = n - 2; i >= 0; --i) {
if (A[i] == -1) {
continue;
}
for (int j = i + 1; j <= min(i + B, n - 1); ++j) {
if (dp[j] == numeric_limits<int>::max()) {
continue;
}
if (A[i] + dp[j] < dp[i]) {
dp[i] = A[i] + dp[j];
next[i] = j;
}
}
}
if (dp[0] == numeric_limits<int>::max()) {
return result;
}
int k = 0;
while (k != -1) {
result.emplace_back(k + 1);
k = next[k];
}
return result;
}
};
Loading...
马建仓 AI 助手
尝试更多
代码解读
代码找茬
代码优化
Python
1
https://gitee.com/wd6/LeetCode-1.git
git@gitee.com:wd6/LeetCode-1.git
wd6
LeetCode-1
LeetCode-1
master

搜索帮助