mirror of
https://github.com/youngyangyang04/leetcode-master.git
synced 2025-07-08 16:54:50 +08:00
Update 0746.使用最小花费爬楼梯.md
This commit is contained in:
@ -330,22 +330,27 @@ var minCostClimbingStairs = function(cost) {
|
|||||||
|
|
||||||
```typescript
|
```typescript
|
||||||
function minCostClimbingStairs(cost: number[]): number {
|
function minCostClimbingStairs(cost: number[]): number {
|
||||||
/**
|
const dp = [0, 0]
|
||||||
dp[i]: 走到第i阶需要花费的最少金钱
|
for (let i = 2; i <= cost.length; i++) {
|
||||||
dp[0]: 0;
|
dp[i] = Math.min(dp[i - 1] + cost[i - 1], dp[i - 2] + cost[i - 2])
|
||||||
dp[1]: 0;
|
}
|
||||||
...
|
return dp[cost.length]
|
||||||
dp[i]: min(dp[i - 1] + cost[i - 1], dp[i - 2] + cost[i - 2]);
|
}
|
||||||
*/
|
```
|
||||||
const dp = [];
|
|
||||||
const length = cost.length;
|
不使用 dp 数组
|
||||||
dp[0] = 0;
|
|
||||||
dp[1] = 0;
|
```typescript
|
||||||
for (let i = 2; i <= length; i++) {
|
function minCostClimbingStairs(cost: number[]): number {
|
||||||
dp[i] = Math.min(dp[i - 1] + cost[i - 1], dp[i - 2] + cost[i - 2]);
|
let dp_before = 0,
|
||||||
}
|
dp_after = 0
|
||||||
return dp[length];
|
for (let i = 2; i <= cost.length; i++) {
|
||||||
};
|
let dpi = Math.min(dp_before + cost[i - 2], dp_after + cost[i - 1])
|
||||||
|
dp_before = dp_after
|
||||||
|
dp_after = dpi
|
||||||
|
}
|
||||||
|
return dp_after
|
||||||
|
}
|
||||||
```
|
```
|
||||||
|
|
||||||
### Rust
|
### Rust
|
||||||
|
Reference in New Issue
Block a user