title: LEETCODE-DAY38
date: 2024-03-30 17:24:33
tags:
今日内容:509. 斐波那契数、70. 爬楼梯 、746. 使用最小花费爬楼梯
T1
class Solution:
def fib(self, n: int) -> int:
if n==0:
return 0
elif n==1:
return 1
else:
res=self.fib(n-1)+self.fib(n-2)
return res
DP写法:
class Solution:
def fib(self, n: int) -> int:
if n==0:
return 0
dp=[0]*(n+1)
dp[0]=0
dp[1]=1
for i in range(2,n+1):
dp[i]=dp[i-1]+dp[i-2]
return dp[n]
T2
class Solution:
def climbStairs(self, n: int) -> int:
if n<=2:
return n
dp=[0 for _ in range(n+1)]
dp[1]=1
dp[2]=2
for i in range(3,n+1):
dp[i]=dp[i-1]+dp[i-2]
return dp[n]
T3
class Solution:
def minCostClimbingStairs(self, cost: List[int]) -> int:
l=len(cost)
dp=[0 for _ in range(l+1)]
for i in range(2,l+1):
dp[i]=min(dp[i-1]+cost[i-1],dp[i-2]+cost[i-2])
return dp[l]