leetcode279 Perfect Squares

 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

leetcode279 Perfect Squares的相关文章

hdu 3524 Perfect Squares 推公式求逆元

Perfect Squares Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others) Total Submission(s): 501    Accepted Submission(s): 272 Problem Description A number x is called a perfect square if there exists an integer b satisfy

Light OJ 1288 Subsets Forming Perfect Squares 高斯消元求矩阵的秩

题目来源:Light OJ 1288 Subsets Forming Perfect Squares 题意:给你n个数 选出一些数 他们的乘积是完全平方数 求有多少种方案 思路:每个数分解因子 每隔数可以选也可以不选 0 1表示 然后设有m种素数因子 选出的数组成的各个因子的数量必须是偶数 组成一个m行和n列的矩阵 每一行代表每一种因子的系数 解出自由元的数量 #include <cstdio> #include <cstring> #include <algorithm&g

CF914A Perfect Squares

CF914A Perfect Squares 题意翻译 给定一组有n个整数的数组a1,a2,…,an.找出这组数中的最大非完全平方数. 完全平方数是指有这样的一个数x,存在整数y,使得x=y^2y2 . 输入格式 第一行输入一个单独的整数n(1<=n<=1000 ),n为该组数的数量. 接下来有n个整数,a1,a2,an(-10^6<=a<=10^6) 数据保证至少存在一个整数是非完全平方数. 题目描述 Given an array a_{1},a_{2},...,a_{n}a1?

Lintcode Perfect Squares

Given a positive integer n, find the least number of perfect square numbers (for example,1, 4, 9, 16, ...) which sum to n. Given n = 12, return 3 because 12 = 4 + 4 + 4Given n = 13, return 2 because 13 = 4 + 9 刷这道题目发现网上有四种解法 http://www.cnblogs.com/gr

[LeetCode] Perfect Squares

Given a positive integer n, find the least number of perfect square numbers (for example, 1, 4, 9, 16, ...) which sum to n. For example, given n = 12, return 3 because 12 = 4 + 4 + 4; given n = 13, return 2 because 13 = 4 + 9. Credits: Special thanks

一、Perfect Squares 完全平方数

h3 { background-color: palegreen } 一原题 Given a positive integer n, find the least number of perfect square numbers (for example, 1, 4, 9, 16, ...) which sum to n. For example, given n = 12, return 3 because 12 = 4 + 4 + 4; given n = 13, return 2 beca

279. Perfect Squares

Total Accepted: 48616 Total Submissions: 142008 Difficulty: Medium Given a positive integer n, find the least number of perfect square numbers (for example, 1, 4, 9, 16, ...) which sum to n. For example, given n = 12, return 3 because 12 = 4 + 4 + 4;

LeetCode 279. Perfect Squares

Question: Given a positive integer n, find the least number of perfect square numbers (for example, 1, 4, 9, 16, ...) which sum to n. For example, given n = 12, return 3 because 12 = 4 + 4 + 4; given n = 13, return 2 because 13 = 4 + 9. Code: public

Perfect Squares

Given a positive integer n, find the least number of perfect square numbers (for example, 1, 4, 9, 16, ...) which sum to n. Example Given n = 12, return 3 because 12 = 4 + 4 + 4Given n = 13, return 2 because 13 = 4 + 9 分析: 首先把所有 1-n 的square numbers找出