leetcode279. Perfect Squares
learn from 
- DP
class Solution {
public:
int numSquares(int n) {
if(n<=0)return 0;
int * dp = new int[n+1]();
for(int i=1;i<=n;i++){
dp[i]= INT_MAX;
for(int j =1;j*j <=i;j++){
int tmp = dp[i-j*j]+1;
dp[i]=dp[i]<tmp?dp[i]:tmp;
}
}
//for(int i=0;i<=n;i++)cout<< dp[i]<<' ';
return dp[n];
}
};
- BFS
class Solution {
public:
int numSquares(int n) {
if (n <= 0)return 0;
else if(pow((int)sqrt(n),2) == n)return 1;
vector<int> perfectSquares;//candinates
vector<int> cntPerfectSquares(n);//count_index
for (int i = 1; i*i <= n; i++)
{
perfectSquares.push_back(i*i);
cntPerfectSquares[i*i - 1] = 1;
}
queue<int> searchQ;
for (auto& i : perfectSquares) searchQ.push(i);
int currCntPerfectSquares = 1;
while (!searchQ.empty())//BFS
{
currCntPerfectSquares++;
int searchQSize = searchQ.size();
for (int i = 0; i < searchQSize; i++)
{
int tmp = searchQ.front();
for (auto& j : perfectSquares)
{
if (tmp + j == n)return currCntPerfectSquares;
else if ((tmp + j < n) && (cntPerfectSquares[tmp + j - 1] == 0))
{
cntPerfectSquares[tmp + j - 1] = currCntPerfectSquares;
searchQ.push(tmp + j);
}
else if (tmp + j > n)break;
}
searchQ.pop();
}
}
return 0;
}
};
leetcode279. Perfect Squares的更多相关文章
- Leetcode279. Perfect Squares完全平方数
给定正整数 n,找到若干个完全平方数(比如 1, 4, 9, 16, ...)使得它们的和等于 n.你需要让组成和的完全平方数的个数最少. 示例 1: 输入: n = 12 输出: 3 解释: 12 ...
- [LintCode] Perfect Squares 完全平方数
Given a positive integer n, find the least number of perfect square numbers (for example, 1, 4, 9, 1 ...
- LeetCode Perfect Squares
原题链接在这里:https://leetcode.com/problems/perfect-squares/ 题目: Given a positive integer n, find the leas ...
- Perfect Squares
Perfect Squares Total Accepted: 18854 Total Submissions: 63048 Difficulty: Medium Given a positive i ...
- CF914A Perfect Squares
CF914A Perfect Squares 题意翻译 给定一组有n个整数的数组a1,a2,…,an.找出这组数中的最大非完全平方数. 完全平方数是指有这样的一个数x,存在整数y,使得x=y^2y2 ...
- Light OJ 1288 Subsets Forming Perfect Squares 高斯消元求矩阵的秩
题目来源:Light OJ 1288 Subsets Forming Perfect Squares 题意:给你n个数 选出一些数 他们的乘积是全然平方数 求有多少种方案 思路:每一个数分解因子 每隔 ...
- [LeetCode] 0279. Perfect Squares 完全平方数
题目 Given a positive integer n, find the least number of perfect square numbers (for example, 1, 4, 9 ...
- LeetCode 279. 完全平方数(Perfect Squares) 7
279. 完全平方数 279. Perfect Squares 题目描述 给定正整数 n,找到若干个完全平方数(比如 1, 4, 9, 16, ...)使得它们的和等于 n.你需要让组成和的完全平方数 ...
- Leetcode之广度优先搜索(BFS)专题-279. 完全平方数(Perfect Squares)
Leetcode之广度优先搜索(BFS)专题-279. 完全平方数(Perfect Squares) BFS入门详解:Leetcode之广度优先搜索(BFS)专题-429. N叉树的层序遍历(N-ar ...
随机推荐
- eclipse 每次切换工作空间都要重新配置
首先,导出T1中的配置打开T1,选择file --> Export --> 在弹出框中选择General 下的preference --> next --> 在export p ...
- hdu 1532 Dinic模板(小白书)
hdu1532 输入n,m. n条边,m个点,之后给出a到b的容量,求1到m的最大流. 注意:Dinic只能调用一次,因为原理是改变cap的值,如果调用多次一样的,那么第一次会对,其余的都会是0,因为 ...
- [实变函数]2.1 度量空间 (metric space), $n$ 维 Euclidean 空间
1 回忆: $$\bex \lim_{n\to\infty}a_n=a\lra \forall\ \ve>0,\ \exists\ N,\ \forall\ n\geq N,\mbo ...
- EXISTS、EXCEPT、INTERSECT 运算符
转:http://www.cnblogs.com/WizardWu/archive/2011/10/01/2197147.html EXISTS运算符 EXISTS 可称之为运算符,有些书称它为关键词 ...
- dos查看端口
C:\Documents and Settings\Administrator>netstat -an | findstr "22" TCP 192.168.16.2 ...
- oracle查看所有表的数据量
源地址:http://blog.csdn.net/zhanggnol/article/details/6683697 select t.table_name,t.num_rows from user_ ...
- 把Nginx加为系统服务(service nginx start/stop/restart)
1.编写脚本,名为nginx #!/bin/sh # # nginx - this script starts and stops the nginx daemon # # chkconfig: - ...
- 标准化命名CSS类,持续更新
放链接.持续化更新,以后可能会用上.https://github.com/zhangxinxu/zxx.lib.css/blob/master/zxx.lib.css
- rsync 目录 斜杠
源: 不带:同步 目录和内容 带/: 只同步内容 target目录: 待. -Warv --delete -W, --whole-file copy files whole ( ...
- DISTINCT后按照DISTINCT之前的某列进行排序
SELECT 行业名称 FROM 评分标准 GROUP BY 行业名称 ORDER BY MAX(行业ID) DESC