update 0746.使用最小花费爬楼梯:更换代码为新的题目描述版本

This commit is contained in:
Yuhao Ju
2022-12-19 16:48:15 +08:00
committed by GitHub
parent 9a70ce0e8f
commit 889f6724f7

View File

@ -83,7 +83,7 @@ dp[i - 2] 跳到 dp[i] 需要花费 dp[i - 2] + cost[i - 2]。
那么 dp[0] 应该是多少呢? 根据dp数组的定义到达第0台阶所花费的最小体力为dp[0]那么有同学可能想那dp[0] 应该是 cost[0],例如 cost = [1, 100, 1, 1, 1, 100, 1, 1, 100, 1] 的话dp[0] 就是 cost[0] 应该是1。
这里就要说名了,本题力扣为什么改题意,而且修改题意之后 就清晰很多的原因了。
这里就要说本题力扣为什么改题意,而且修改题意之后 就清晰很多的原因了。
新题目描述中明确说了 “你可以选择从下标为 0 或下标为 1 的台阶开始爬楼梯。” 也就是说 从 到达 第 0 个台阶是不花费的,但从 第0 个台阶 往上跳的话,需要花费 cost[0]。
@ -101,7 +101,7 @@ dp[i - 2] 跳到 dp[i] 需要花费 dp[i - 2] + cost[i - 2]。
> **但是稍稍有点难度的动态规划,其遍历顺序并不容易确定下来**。
> 例如01背包都知道两个for循环一个for遍历物品嵌套一个for遍历背包容量那么为什么不是一个for遍历背包容量嵌套一个for遍历物品呢 以及在使用一维dp数组的时候遍历背包容量为什么要倒序呢
**这些都遍历顺序息息相关。当然背包问题后续「代码随想录」都会重点讲解的!**
**这些都遍历顺序息息相关。当然背包问题后续「代码随想录」都会重点讲解的!**
5. 举例推导dp数组
@ -182,7 +182,7 @@ public:
## 总结
大家可以发现这道题目相对于 昨天的[动态规划:爬楼梯](https://programmercarl.com/0070.爬楼梯.html)难了一点,但整体思路是一样。
大家可以发现这道题目相对于 昨天的[动态规划:爬楼梯](https://programmercarl.com/0070.爬楼梯.html)难了一点,但整体思路是一样
从[动态规划:斐波那契数](https://programmercarl.com/0509.斐波那契数.html)到 [动态规划:爬楼梯](https://programmercarl.com/0070.爬楼梯.html)再到今天这道题目,录友们感受到循序渐进的梯度了嘛。
@ -243,43 +243,43 @@ class Solution {
```python
class Solution:
def minCostClimbingStairs(self, cost: List[int]) -> int:
dp = [0] * (len(cost))
dp[0] = cost[0]
dp[1] = cost[1]
for i in range(2, len(cost)):
dp[i] = min(dp[i - 1], dp[i - 2]) + cost[i]
return min(dp[len(cost) - 1], dp[len(cost) - 2])
dp = [0] * (len(cost) + 1)
dp[0] = 0
dp[1] = 0
for i in range(2, len(cost) + 1):
dp[i] = min(dp[i - 1] + cost[i - 1], dp[i - 2] + cost[i-2])
return dp[len(cost)]
```
### Go
```Go
func minCostClimbingStairs(cost []int) int {
dp := make([]int, len(cost))
dp[0], dp[1] = cost[0], cost[1]
for i := 2; i < len(cost); i++ {
dp[i] = min(dp[i-1], dp[i-2]) + cost[i]
}
return min(dp[len(cost)-1], dp[len(cost)-2])
f := make([]int, len(cost) + 1)
f[0], f[1] = 0, 0
for i := 2; i <= len(cost); i++ {
f[i] = min(f[i-1] + cost[i-1], f[i-2] + cost[i-2])
}
return f[len(cost)]
}
func min(a, b int) int {
if a < b {
return a
}
return b
if a < b {
return a
}
return b
}
```
### Javascript
```Javascript
var minCostClimbingStairs = function(cost) {
const dp = [ cost[0], cost[1] ]
for (let i = 2; i < cost.length; ++i) {
dp[i] = Math.min(dp[i -1] + cost[i], dp[i - 2] + cost[i])
const n = cost.length;
const dp = new Array(n + 1);
dp[0] = dp[1] = 0;
for (let i = 2; i <= n; ++i) {
dp[i] = Math.min(dp[i -1] + cost[i - 1], dp[i - 2] + cost[i - 2])
}
return Math.min(dp[cost.length - 1], dp[cost.length - 2])
return dp[n]
};
```
@ -289,19 +289,19 @@ var minCostClimbingStairs = function(cost) {
function minCostClimbingStairs(cost: number[]): number {
/**
dp[i]: 走到第i阶需要花费的最少金钱
dp[0]: cost[0];
dp[1]: cost[1];
dp[0]: 0;
dp[1]: 0;
...
dp[i]: min(dp[i - 1], dp[i - 2]) + cost[i];
dp[i]: min(dp[i - 1] + cost[i - 1], dp[i - 2] + cost[i - 2]);
*/
const dp: number[] = [];
const length: number = cost.length;
dp[0] = cost[0];
dp[1] = cost[1];
const dp = [];
const length = cost.length;
dp[0] = 0;
dp[1] = 0;
for (let i = 2; i <= length; i++) {
dp[i] = Math.min(dp[i - 1], dp[i - 2]) + cost[i];
dp[i] = Math.min(dp[i - 1] + cost[i - 1], dp[i - 2] + cost[i - 2]);
}
return Math.min(dp[length - 1], dp[length - 2]);
return dp[length];
};
```