给定正整数 n,找到若干个完全平方数(比如 1, 4, 9, 16, ...)使得它们的和等于 n。你需要让组成和的完全平方数的个数最少。

示例 1:

输入: n = 12 输出: 3 解释: 12 = 4 + 4 + 4.

示例 2:

输入: n = 13 输出: 2 解释: 13 = 4 + 9.

class Solution {
public:
int numSquares(int n)
{
vector<int> squares;
for(int i = 1; i * i <= n; i++)
{
squares.push_back(i * i);
}
vector<int> dp(n + 1, INT_MAX);
dp[0] = 0;
for(int i = 1; i <= n; i++)
{
for(int j = squares.size() - 1; j >= 0; j--)
{
if(squares[j] <= i && dp[i - squares[j]] != INT_MAX)
{
dp[i] = min(dp[i - squares[j]] + 1, dp[i]);
}
}
}
return dp[n];
}
};

Leetcode279. Perfect Squares完全平方数的更多相关文章

  1. [LeetCode] 0279. Perfect Squares 完全平方数

    题目 Given a positive integer n, find the least number of perfect square numbers (for example, 1, 4, 9 ...

  2. [LintCode] Perfect Squares 完全平方数

    Given a positive integer n, find the least number of perfect square numbers (for example, 1, 4, 9, 1 ...

  3. [LeetCode] Perfect Squares 完全平方数

    Given a positive integer n, find the least number of perfect square numbers (for example, 1, 4, 9, 1 ...

  4. 一、Perfect Squares 完全平方数

    一原题 Given a positive integer n, find the least number of perfect square numbers (, , , , ...) which ...

  5. [LeetCode] 279. Perfect Squares 完全平方数

    Given a positive integer n, find the least number of perfect square numbers (for example, 1, 4, 9, 1 ...

  6. leetcode279. Perfect Squares

    learn from DP class Solution { public: int numSquares(int n) { if(n<=0)return 0; int * dp = new i ...

  7. LeetCode 279. 完全平方数(Perfect Squares) 7

    279. 完全平方数 279. Perfect Squares 题目描述 给定正整数 n,找到若干个完全平方数(比如 1, 4, 9, 16, ...)使得它们的和等于 n.你需要让组成和的完全平方数 ...

  8. Leetcode之广度优先搜索(BFS)专题-279. 完全平方数(Perfect Squares)

    Leetcode之广度优先搜索(BFS)专题-279. 完全平方数(Perfect Squares) BFS入门详解:Leetcode之广度优先搜索(BFS)专题-429. N叉树的层序遍历(N-ar ...

  9. CF914A Perfect Squares

    CF914A Perfect Squares 题意翻译 给定一组有n个整数的数组a1,a2,…,an.找出这组数中的最大非完全平方数. 完全平方数是指有这样的一个数x,存在整数y,使得x=y^2y2  ...

随机推荐

  1. 【JZOJ6384】珂学家

    description analysis 注意配出来的饮料不可以再配成其他饮料,所以肯定有\(O(n^2)\)的枚举 而且可口度两两互不相同,搞得我以为这是神仙题 考虑把两个试剂\([l_1,r_1] ...

  2. MySQL基础知识 数据库 数据表

    1.数据库结构 库 表 数据 2. sql(structured query language)结构化查询语言 管理数据库 管理表 管理数据 3.数据库 增删改查 增 create database  ...

  3. 两个对象值相同 (x.equals(y) == true),但却可有不同的 hash code,这句话对不对?

    不对,如果两个对象x和y满足x.equals(y) == true,它们的哈希码(hash code)应当相同.Java对于eqauls方法和hashCode方法是这样规定的: (1)如果两个对象相同 ...

  4. “fixed+relative≈≈absolute”——对BFC的再次思考

    好久没写博客了,刚好今天跨年夜没约到什么妹子,在家宅着不如写点东西好了. 需求 昨天晚上,给公司年会做一个移动端的投票页面,遇到一个UI优化的问题: · 正文内容少于一屏时,投票提交按钮固定显示在页面 ...

  5. DEDECMS织梦后台更新网站栏目无反应一键更新无响应的解决方法

    很多站长朋友反应,经常会遇到DEDECMS织梦后台更新网站栏目无反应和一键更新无响应的问题,这个问题的所在就是在于恢复了数据或者覆盖了织梦后台文件之后,点击一键更新完全没反应,或者生成栏目的时候其他都 ...

  6. spark jdk8 单词统计示例

    在github上有spark-java8 实例地址: https://github.com/ypriverol/spark-java8 https://github.com/ihr/java8-spa ...

  7. vue/cli 3.0脚手架搭建

    在vue 2.9.6中,搭建vue-cli脚手架的流程是这样的: 首先 全局安装vue-cli,在cmd中输入命令: npm install --global vue-cli  安装成功:  安装完成 ...

  8. 查看python安装位置和已安装库的相关操作

    打开cmd.exe, *查看python安装位置 where python *查看已安装库 pip list 或者pip freeze *查看可以更新的第三方库 pip list --outdated ...

  9. Type.GetType(string.contains(','))

    例如 Type type = Type.GetType("ACalCoreServiceLib.BaseService,ACalCoreServiceLib"); 里面的ACalC ...

  10. 你必须知道的.NET Day1