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 ...
随机推荐
- SparkSQL项目中的应用
Spark是一个通用的大规模数据快速处理引擎.可以简单理解为Spark就是一个大数据分布式处理框架.基于内存计算的Spark的计算速度要比Hadoop的MapReduce快上100倍以上,基于磁盘的计 ...
- storyBoard中切换应用启动的切入点方法
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launc ...
- cmd 登录oracle
源地址:http://zhidao.baidu.com/link?url=mehN7bFY14DGH6DwhpbJnAbzb_fI3WbQn2-WqVInyyqHkfYlZSfu7GQVjQgQoPV ...
- C++学习14 继承的概念及语法
继承是类与类之间的关系,是一个很简单很直观的概念,与现实世界中的继承(例如儿子继承父亲财产)类似. 继承(Inheritance)可以理解为一个类从另一个类获取成员变量和成员函数的过程.例如类B继承于 ...
- Understanding Asynchronous IO With Python 3.4's Asyncio And Node.js
[转自]http://sahandsaba.com/understanding-asyncio-node-js-python-3-4.html Introduction I spent this su ...
- [SQL] 不合并重复数据 union all
select * from A union select * from B --不合并重复行 select * from A union all select * from B --如果要对字段进行排 ...
- FreeMarker中List排序
有时候需要在页面上对list排序,虽然也可以在后台代码中完成,但这个可能要看具体情况.排序的样本代码如下: <#list resultMap.topViewList?sort_by(" ...
- Hibernate注解:一对一主键关联
情形:两个表,my_site和my_site_company,通过主键site_id唯一关联.my_site的主键是自动增加,my_site_company的主键依赖于my_site. # # Sou ...
- poj 1459 Power Network : 最大网络流 dinic算法实现
点击打开链接 Power Network Time Limit: 2000MS Memory Limit: 32768K Total Submissions: 20903 Accepted: ...
- Tools之FindBugs
我们先来看FindBugs.(因为我们的错误通常分为两种,静态错误和动态错误也就是运行时的,而FindBugs是一个Java代码静态错误分析工具.) 1) 安装 Help->SoftWare U ...