Skip to content

Commit

Permalink
Merge pull request youngyangyang04#1948 from HotPotJ/master
Browse files Browse the repository at this point in the history
为【0749.使用最小花费爬楼梯】提供了另一种动态规划方程的思路 Go语言班班
  • Loading branch information
youngyangyang04 authored Mar 23, 2023
2 parents edea08e + d34e4a8 commit 5d33abb
Showing 1 changed file with 27 additions and 0 deletions.
27 changes: 27 additions & 0 deletions problems/0746.使用最小花费爬楼梯.md
Original file line number Diff line number Diff line change
Expand Up @@ -284,6 +284,33 @@ func min(a, b int) int {
return b
}
```
``` GO
第二种思路: dp[i]表示从i层起跳所需要支付的最小费用
递推公式:
i<n :dp[i] = min(dp[i-1],dp[i-2])+cost[i]
i==n:dp[i] = min(dp[i-1],dp[i-2]) (登顶)

func minCostClimbingStairs(cost []int) int {
n := len(cost)
dp := make([]int, n+1)
dp[0], dp[1] = cost[0], cost[1]
for i := 2; i <= n; i++ {
if i < n {
dp[i] = min(dp[i-1], dp[i-2]) + cost[i]
} else {
dp[i] = min(dp[i-1], dp[i-2])
}
}
return dp[n]
}
func min(a, b int) int {
if a < b {
return a
}
return b
}
```


### Javascript
```Javascript
Expand Down

0 comments on commit 5d33abb

Please sign in to comment.