1 """ 2 Given a positive integer n, find the least number of perfect square numbers (for example, 1, 4, 9, 16, ...) which sum to n. 3 Example 1: 4 Input: n = 12 5 Output: 3 6 Explanation: 12 = 4 + 4 + 4. 7 Example 2: 8 Input: n = 13 9 Output: 2 10 Explanation: 13 = 4 + 9. 11 """ 12 """ 13 dp[0] = 0 14 dp[1] = dp[0]+1 = 1 15 dp[2] = dp[1]+1 = 2 16 dp[3] = dp[2]+1 = 3 17 dp[4] = Min{ dp[4-1*1]+1, dp[4-2*2]+1 } 18 = Min{ dp[3]+1, dp[0]+1 } 19 = 1 20 dp[5] = Min{ dp[5-1*1]+1, dp[5-2*2]+1 } 21 = Min{ dp[4]+1, dp[1]+1 } 22 = 2 23 . 24 . 25 . 26 dp[13] = Min{ dp[13-1*1]+1, dp[13-2*2]+1, dp[13-3*3]+1 } 27 = Min{ dp[12]+1, dp[9]+1, dp[4]+1 } 28 = 2 29 . 30 . 31 . 32 dp[n] = Min{ dp[n - i*i] + 1 }, n - i*i >=0 && i >= 1 33 dp[n] 表示以n为和的最少平方的和的个数(所求)。 34 dp 数组所有下标已经为完全平方数的数(如1,4,9...)置为 1,加一层 j 遍历找到当前 i 下长度最小的组合。 35 动态方程的意思是:对于每个 i ,比 i 小一个完全平方数的那些数中最小的个数+1就是所求,也就是 dp [ i - j * j ] + 1 。 36 """ 37 class Solution1: 38 def numSquares(self, n): 39 dp = [float(‘inf‘)]*(n+1) 40 i = 1 41 while i*i <= n: 42 dp[i*i] = 1 43 i += 1 44 for i in range(1, n+1): 45 j = 1 46 while j*j <= i: 47 dp[i] = min(dp[i], dp[i-j*j]+1) 48 j += 1 49 return dp[n]
原文地址:https://www.cnblogs.com/yawenw/p/12359346.html
时间: 2024-10-29 13:25:41