class Solution(object):
_dp = [0]
def numSquares(self, n):
"""
:type n: int
:rtype: int
"""
dp = self._dp
while len(dp) <= n:
dp += min(dp[-i*i] for i in range(1, int(len(dp)**0.5+1))) + 1,
_dp = [0]
def numSquares(self, n):
"""
:type n: int
:rtype: int
"""
dp = self._dp
while len(dp) <= n:
dp += min(dp[-i*i] for i in range(1, int(len(dp)**0.5+1))) + 1,
return dp[n]
https://leetcode.com/tag/math/
本文介绍了一种使用动态规划解决LeetCode数学题目的方法。通过维护一个动态规划数组,可以高效地找出完全平方数的最小数量,使得这些完全平方数之和等于给定的正整数n。
525

被折叠的 条评论
为什么被折叠?



