题目链接:https://leetcode.cn/problems/perfect-squares/
难度:中等

描述:
给你一个整数 n ,返回 和为 _n_ 的完全平方数的最少数量

题解

  1. class Solution:
  2. def numSquares(self, n: int) -> int:
  3. # r[i]是和为i的完全平方数的最少数量
  4. r = list(range(n+1))
  5. for i in range(1, n+1):
  6. j = 1
  7. while j * j <= i:
  8. r[i] = min(r[i], 1 + r[i - j*j])
  9. j += 1
  10. return r[-1]