class Solution(object):
def numSquares(self, n):
"""
:type n: int
:rtype: int
"""
# invalid input:
if n < 1:
return 0
# valid input:
# dp:
dp = range(n+1)
num = 2
while num <= n:
count = 5
i = 1
while num  i ** 2 >= 0:
count = min(count, dp[num  i**2] + 1)
i += 1
dp[num] = count
num += 1
return dp[1]
HELP! Why Python dp solution TLE?


Try my fix:
https://discuss.leetcode.com/topic/53829/pythondpacceptedthesecrettofixthepythontleerror