The Fibonacci numbers, commonly denoted F(n)
form a sequence, called the Fibonacci sequence, such that each number is the sum of the two preceding ones, starting from 0
and 1
. That is,
F(0) = 0, F(1) = 1 F(n) = F(n - 1) + F(n - 2), for n > 1.
Given n
, calculate F(n)
.
way1:
Time complexity: O(n)
Space complexity: O(n)
class Solution:
def fib(self, n: int) -> int:
if n <= 1:
return n
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]
way 2:
Time complexity: O(n)
Space complexity: O(1)
class Solution:
def fib(self, n: int) -> int:
if n <= 1:
return n
dp = [0] * (n + 1)
dp[0] = 0
dp[1] = 1
for i in range(2, n + 1):
dp[0], dp[1] = dp[1], dp[0] + dp[1]
return dp[1]
way 3: recursion
Time complexity: O(2^n)
Space complexity: O(n)
class Solution:
def fib(self, n: int) -> int:
if n < 2:
return n
return self.fib(n - 1) + self.fib(n - 2)
You are climbing a staircase. It takes n
steps to reach the top.
Each time you can either climb 1
or 2
steps. In how many distinct ways can you climb to the top?
way1:
Time complexity: O(n)
Space complexity: O(n)
class Solution:
def climbStairs(self, n: int) -> int:
if n < 3:
return n
dp = [0] * (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]
way2:
Time complexity: O(n)
Space complexity: O(1)
class Solution:
def climbStairs(self, n: int) -> int:
if n < 3:
return n
dp = [0] * (n + 1)
dp[1] = 1
dp[2] = 2
for i in range(3, n + 1):
dp[1], dp[2] = dp[2], dp[1] + dp[2]
return dp[2]
If you can go up (0 to m) steps each time: !!!!!!!
class Solution:
def climbStairs(self, n: int) -> int:
dp = [0] * (n + 1)
dp[0] = 1
for i in range(1, n + 1):
for j in range(1, m + 1): # 每次只能走2 步,就把m变成2
if i >= j:
dp[i] += dp[i-j]
return dp[n]
You are given an integer array cost
where cost[i]
is the cost of ith
step on a staircase. Once you pay the cost, you can either climb one or two steps.
You can either start from the step with index 0
, or the step with index 1
.
Return the minimum cost to reach the top of the floor.
my way:
Time complexity: O(n)
Space complexity: O(n)
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] = cost[i] + min(dp[i - 1], dp[i - 2])
return min(dp[len(cost) - 1], dp[len(cost) - 2])
way1:
Time complexity: O(n)
Space complexity: O(n)
class Solution:
def minCostClimbingStairs(self, cost: List[int]) -> int:
dp = [0] * (len(cost) + 1)
dp[0] = 0 # 初始值,表示从起点开始不需要花费体力
dp[1] = 0 # 初始值,表示经过第一步不需要花费体力
for i in range(2, len(cost) + 1):
# 在第i步,可以选择从前一步(i-1)花费体力到达当前步,或者从前两步(i-2)花费体力到达当前步
# 选择其中花费体力较小的路径,加上当前步的花费,更新dp数组
dp[i] = min(dp[i - 1] + cost[i - 1], dp[i - 2] + cost[i - 2])
return dp[len(cost)] # 返回到达楼顶的最小花费
way2:
Time complexity: O(n)
Space complexity: O(1)
class Solution:
def minCostClimbingStairs(self, cost: List[int]) -> int:
dp0 = 0 # 初始值,表示从起点开始不需要花费体力
dp1 = 0 # 初始值,表示经过第一步不需要花费体力
for i in range(2, len(cost) + 1):
# 在第i步,可以选择从前一步(i-1)花费体力到达当前步,或者从前两步(i-2)花费体力到达当前步
# 选择其中花费体力较小的路径,加上当前步的花费,得到当前步的最小花费
dpi = min(dp1 + cost[i - 1], dp0 + cost[i - 2])
dp0 = dp1 # 更新dp0为前一步的值,即上一次循环中的dp1
dp1 = dpi # 更新dp1为当前步的最小花费
return dp1 # 返回到达楼顶的最小花费