应用题-动态规划-使用最小花费爬楼梯
- 到达楼顶的最小费用,相当于跳到n+1阶的最小费用;
- dp( n ) = min( n-2阶前的最小费用 + n-2阶跳起的费用, n-1阶跳起的费用 + n-1阶跳起的费用 )
- cost是每一层台阶跳起的费用,cost[0]相当于第1阶台阶跳起的费用,cost[i]相当于第i+1阶台阶跳起的费用;
```python
class Solution:
# 一个整数数组 cost ,其中 cost[i] 是从楼梯第 i 个台阶向上爬需要支付的费用。
# 一旦支付此费用,即可选择向上爬一个或者两个台阶。
# 可以选择从下标为 0 或下标为 1 的台阶开始爬楼梯。
# 请计算并返回达到楼梯顶部的最低花费。
# 2 <= cost.length <= 1000
# 0 <= cost[i] <= 999
def minCostClimbingStairs(self, cost: List[int]) -> int:
n = len( cost )
# 从所有爬楼梯的方式中 选择最小费用
# 注:达到楼顶相当于跳到n+1
# dp( i ) 处于第i阶时 收取的最小费用
x = 0 # 跳到第1阶最小费用
y = 0 # 跳到第2阶最小费用
for i in range( 3, n + 2 ):
# 由于cost下标从0开始 cost[i]就是cost[ i+1 ]的费用
z = min( x + cost[ i - 3 ], y + cost[ i - 2 ] )
x = y
y = z
return y
```