赞
踩
dp[i]:爬到有i个台阶的楼顶,有dp[i]种方法
def solve(n,m):
dp=[0]*(n+1)
dp[0]=1
for j in range(1,n+1):
for i in range(1,m+1):
if (j-i)>=0:
dp[j]+=dp[j-i]
return dp[n]
n,m=list(map(int,input().split()))
print(n,m)
res=solve(n,m)
print(res)
class Solution(object):
def coinChange(self, coins, amount):
"""
:type coins: List[int]
:type amount: int
:rtype: int
"""
# dp[i]:凑成总金额i所需的最少的硬币个数
dp=[float('inf')]*(amount+1)
dp[0]=0
for i in range(len(coins)):
for j in range(coins[i],amount+1):
dp[j]=min(dp[j],dp[j-coins[i]]+1)
return -1 if dp[amount]==float('inf') else dp[amount]
class Solution(object):
def numSquares(self, n):
"""
:type n: int
:rtype: int
"""
# dp[j]:和为 j的完全平方数的最少数量
dp=[float('inf')]*(n+1)
dp[0]=0
for i in range(1,int(sqrt(n))+1):
for j in range(i*i,n+1):
dp[j]=min(dp[j],dp[j-i*i]+1)
return dp[n]
Copyright © 2003-2013 www.wpsshop.cn 版权所有,并保留所有权利。