LlintCode:完美平方
来源:互联网 发布:湖北广电网络官网 编辑:程序博客网 时间:2024/05/21 08:51
完美平方
给一个正整数 n, 找到若干个完全平方数(比如1, 4, 9, ... )使得他们的和等于 n。你需要让平方数的个数最少。
给出 n = 12
, 返回 3
因为 12 = 4 + 4 + 4
。
给出 n = 13
, 返回 2
因为 13 = 4 + 9
。
使用动态规划
class Solution {
public:
/**
* @param n a positive integer
* @return an integer
*/
int numSquares(int n) {
// Write your code here
int *dp = new int [n+1];
dp[0] = 0;
dp[1] = 1;
for(int i=2; i<=n; i++)
{
if(int(sqrt(i))*int(sqrt(i))==i)
{
dp[i] = 1;
continue;
}
int mincount = 0x7fffffff;
for(int j=1; j*j<=i; j++)
{
mincount = min(mincount, dp[j*j]+dp[i-j*j]);
}
dp[i] = mincount;
}
return dp[n];
}
};
- LlintCode:完美平方
- 完美平方
- LintCode:完美平方
- Java实现-完美平方
- Lint-Code完美平方
- lintcode--完美平方
- LintCode513: 完美平方
- 完美平方-LintCode
- 完美平方数
- [51nod1323]完美平方
- 百练 2810 完美平方
- 创新工场完美平方数
- 51nod 1323 完美平方
- [Leetcode] Perfect Squares 完美平方数
- LeetCode 279. Perfect Squares(完美平方)
- leetcode 279. Perfect Squares-完美平方数|动态规划
- 513.Perfect Squares-完美平方(中等题)
- LeetCode-279. Perfect Squares | (JAVA) (完美平方数)
- 使用 Smartmontools 检测硬盘坏道
- Mybatis-基本配置文件
- 腾讯云CentOS7.0使用yum安装mysql及使用遇到的问题
- C计算最大回文字串的长度
- 1225 余数之和
- LlintCode:完美平方
- Linux 安装 .NET Core 1.0 SDK
- 博弈sg函数模板
- Codeforces Round #382 (Div. 1) A. Tennis Championship
- Error: must call SetScrollSizes() or SetScaleToFitSize()问题的解决
- Java获取函数参数名称的几种方法
- 15Spring整合HIbernate&Ibatis(不完整版)
- codeforces 739B B. Alyona and a tree
- AE基础动画参数之制作跳动音符