classSolution:defcoinChange(self, coins: List[int], amount:int)->int:@cache# 缓存装饰器,避免重复计算 dfs 的结果(记忆化)defdfs(i:int, c:int)->int:if i <0:return0if c ==0else infif c < coins[i]:return dfs(i -1, c)returnmin(dfs(i -1, c), dfs(i, c - coins[i])+1)ans = dfs(len(coins)-1, amount)return ans if ans < inf else-1
classSolution:defnumSquares(self, n:int)->int:dp =[0]*(n+1)for i inrange(1, n +1):if i **0.5%1==0:dp[i]=1else:dp[i]=1+min([dp[i-j*j]for j inrange(1,int(i**0.5)+1)])return dp[-1]