leetcode-Perfect Squares-279

输入整数n,求完全平方数的和等于n中完全平方数用得最少的个数

直观想法搜索,先处理n以内的完全平方数,然后搜索,但效率不行,整数范围内的完全平方数有40000多个,用搜索的方法相当于求集合的子集个数,时间是2^n,肯定超时

正解:dp

dp[i]表示完全平方数和等于i的最优解

dp[i]=min(dp[i-j*j]+1)

时间N*sqrt(N)

 1 class Solution {
 2 public:
 3     int numSquares(int n) {
 4         if(n==0) return 0;
 5         int dp[n+1];
 6         memset(dp,0,sizeof(dp));
 7         dp[1]=1;
 8         for(int i=2;i<=n;i++){
 9             int mi=INT_MAX;
10             int ok=0;
11             for(int j=1;j*j<=i;j++){
12                 if(j*j==i){
13                     dp[i]=1;
14                     ok=1;
15                     break;
16                 }
17                 mi=min(mi,dp[i-j*j]+1);
18             }
19             if(!ok) dp[i]=mi;
20         }
21         return dp[n];
22     }
23 };
时间: 2024-10-14 22:18:20

leetcode-Perfect Squares-279的相关文章

【leetcode】Perfect Squares (#279)

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. 解析: 利用动态规划解决此问题:对于要求的当前节

[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

[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

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?

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

[email&#160;protected] [279]Perfect Squares

https://leetcode.com/problems/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, retur

【Leetcode】Perfect Squares

题目链接:https://leetcode.com/problems/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 = 

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;