给定正整数 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. git连接gitee笔记

    #首先参照 https://blog.csdn.net/zhangyu4863/article/details/80427289 #然后需要注意,在办公室无法使用 git remote add ori ...

  2. [JZOJ6340] 【NOIP2019模拟2019.9.4】B

    题目 题目大意 给你个非负整数数列\(a\),每次等概率选择大于零的\(a_i\),使其减\(1\). 问\(a_1\)被减到\(0\)的时候期望经过多少次操作. 思考历程 对于这题的暴力做法,显然可 ...

  3. rancher2.0 自定义应用商店(catalog)

    1.进入自定义应用商店页面 ===================================================== ================================ ...

  4. Django项目:堡垒机(Linux服务器主机管理系统)--03--03堡垒机在Linux系统里记录会话日志02/02

    #main.py #本文件写所有的连接交互动作程序 # ————————————————03堡垒机在Linux系统里记录会话日志 开始———————————————— from Fortress im ...

  5. python实现百度OCR图片识别

    一.直接上代码 import base64 import requests class CodeDemo: def __init__(self,AK,SK,code_url,img_path): se ...

  6. Form-Item Slot 自定义label内容

    <el-form-item> <span slot="label">体   重:</span> <el-input v-model=&qu ...

  7. Android中visibility属性

    Android开发中,大部分控件都有visibility这个属性,其属性有3个分别为“visible ”.“invisible”.“gone”.主要用来设置控制控件的显示和隐藏. 1) 可见(visi ...

  8. vue语法糖

    加冒号,就是个语法糖  两点: 例如 export default { data(){ return { item: { src: 'xxxxx' } } } } <img :src='item ...

  9. ASP.NET的Validform验证表单使用说明

    当我们写提交表单的时候往往需要验证表单是否填写了内容,是否正确,这个插件可以很方便的完成我们需要的验证! 使用方法: 1.引用JS <script type="text/javascr ...

  10. collections,time,random,os, sys 模块的使用

    主要内容:1. 模块的简单认识2. collections模块3. time时间模块4. random模块5. os模块6. sys模块 一. 模块的简单认识什么是模块. 模块就是我们把装有特定功能的 ...