力扣 279. 完全平方数

动态规划

1
2
3
4
5
6
7
8
9
10
def numSquares(n):
# 动态规划
dp = [0 for _ in range(n + 1)]
for i in range(1, n + 1):
dp[i] = i
j = 1
while j * j <= i:
dp[i] = min(dp[i], dp[i - j * j] + 1)
j += 1
return dp[-1]
  • Copyright: Copyright is owned by the author. For commercial reprints, please contact the author for authorization. For non-commercial reprints, please indicate the source.
  • Copyrights © 2022 eightyninth
  • Visitors: | Views:

请我喝杯咖啡吧~

支付宝
微信