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 ...
随机推荐
- storm系统架构学习
Storm架构如下图所示: 1.主控节点(Master Node) 运行Storm nimbus后台服务的节点(Nimbus),它是storm系统的中心,负责接收用户提交的作业(如同spark sub ...
- ylbtech-Unitity-cs:传递的字符串中数字字符的数目
ylbtech-Unitity-cs:传递的字符串中数字字符的数目 1.A,效果图返回顶部 1.B,源代码返回顶部 1.B.1, using System; namespace Functions ...
- bootstrap-响应式图片、辅助类样式
响应式图片: <div class="container"> <!-- img-responsive 响应式图片 --> <div class=&qu ...
- 如何在XAMPP中设置多个网站
xampp 是一个非常方便的本地 apache + php + mysql 的调试环境,在本地安装测试 WordPress 等各种博客.论坛程序非常方便.今天我们来给大家介绍一下,如何使用 XAMPP ...
- [kuangbin带你飞]专题十五 数位DP
ID Origin Title 62 / 175 Problem A CodeForces 55D Beautiful numbers 30 / 84 Problem B HD ...
- [SQL]SQL类似统计功能的sql文
declare @t table(name varchar(),type int) insert into @t union all union all union all union all if ...
- C++primer 练习11.33:实现你自己版本的单词转换程序
// 11_33.cpp : 定义控制台应用程序的入口点. // #include "stdafx.h" #include<iostream> #include< ...
- poj 2632 Crashing Robots
点击打开链接 Crashing Robots Time Limit: 1000MS Memory Limit: 65536K Total Submissions: 6655 Accepted: ...
- Redo与undo在开发中的使用
redo:记录用户的操作.(commit) undo:相反的用操作.(rollback) checkpoint:redo真正写入物理存储.(定时写---定时策略) ================== ...
- 用ie调试的时候显示:脚本调试程序无法连接到目标进程,已附加调试程序。
解决方案如图所示: 解决方案: 在internet的选项工具中选中高级然后去掉禁止脚本调试的情况: