题目链接:Perfect Squares

题目内容:

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

For example, given n = 12, return 3 because 12 = 4 + 4 + 4; given n = 13, return 2 because 13 = 4 + 9.

题目解法:

最初看到这个题,我想到的是回溯法,剪了半天的枝还是各种超时,后来参考了xudli的解法,才知道这道题用动态规划更合适。

我们设d[i]=a表示数字i对应的least number of perfect square numbers为a,则显然d[1]=1为初始条件,接着我们用背包问题的思想,对于i从2到n,尝试向其中放入perfect square number j,j从1开始枚举,每次放入后得到的结果为d[i - j*j]+1,也就是在不放入j的least number基础上+1得到d[i]的最小值,对于不同的j,我们应该选取其中最小的那个,也就是说:d[i] = min{d[i-j*j],j=1,2,3...,j*j<=i}。

最后,d[n]就是结果。

代码如下:

class Solution {
public:int getMin(int a, int b){return a < b ? a : b;}int numSquares(int n) {int *d = new int[n+1];d[1] = 1; // d[i]表示数字i的Prefect Seuares值。for(int i = 2; i <= n; i++){int j = 1;int min = 99999999;while(j*j <= i){if(j*j == i){min = 1;break;}min = getMin(min,d[i-j*j] + 1);j++;}d[i] = min;}return d[n];}
};

转载于:https://www.cnblogs.com/aiwz/p/6154018.html

[LeetCode]Perfect Squares相关推荐

  1. LeetCode Perfect Squares

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

  2. LeetCode -- Perfect Squares

    Question: Given a positive integer n, find the least number of perfect square numbers (for example,  ...

  3. LeetCode Perfect Squares(动态规划)

    题意:给出一个整数,求其等于若干个平方数的和的最小值 思路:用动态规划 具体代码如下: public class Solution {public int numSquares(int n){int[ ...

  4. LeetCode 279. Perfect Squares

    279. Perfect Squares Given a positive integer n, find the least number of perfect square numbers (fo ...

  5. 4kyu Sums of Perfect Squares

    4kyu Sums of Perfect Squares 题目背景: The task is simply stated. Given an integer n (3 < n < 109) ...

  6. leetcode -- 279. Perfect Squares

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

  7. leetcode算法题--Perfect Squares

    原题链接:https://leetcode.com/problems/perfect-squares/ int numSquares(int n) { //0点到n点的最短距离queue<int ...

  8. leetcode 279. Perfect Squares | 279. 完全平方数(动态规划,Java)

    题目 https://leetcode.com/problems/perfect-squares/ 题解:动态规划 参考:[宫水三叶]详解完全背包一维空间优化推导(附背包问题攻略) 首先初始化长度为 ...

  9. 279 Perfect Squares 完美平方数

    给定正整数 n,找到若干个完全平方数(比如 1, 4, 9, 16, ...) 使得他们的和等于 n.你需要让平方数的个数最少. 比如 n = 12,返回 3 ,因为 12 = 4 + 4 + 4 : ...

最新文章

  1. ECSHOP学习笔记
  2. Kali Linux发布2020.1a版本
  3. 使用matlab构建一个信号、添加噪声信号并图示信号
  4. Linux定时任务服务crond
  5. UNITY Profiler 真机调试
  6. mysql 高性能压力测试(总结了好久)
  7. java错误1601解决方法,求助解决错误
  8. 现代软件工程 第十七章 【人、绩效和职业道德】 练习与讨论
  9. confluence启动不起来_汽车上为什么会出现无钥匙启动的功能?存在的意义是什么?...
  10. 残缺、时间一起的爱情
  11. matlab调用system,求助 system函数用法要点
  12. 打印2018年的日历
  13. 关于保险的“损失补偿原则”
  14. 解决zabbix自动发现主机后主机名称是IP地址的问题
  15. 你的导师对你说过什么让你至今难以忘怀的话?
  16. 程序员如何提升自己的能力
  17. 数据清洗 Chapter07 | 简单的数据缺失处理方法
  18. Layer 1: Single Objects
  19. 软件工程——软件总体设计
  20. 为什么‘A‘的ASCII码是65,‘a‘是97呢?

热门文章

  1. 人工智能突破!牛津大学的科学家用机器合成了“类人类思想”
  2. mysql 5.7解压缩_mysql 5.7.22 解压缩安装
  3. 油气储运工程中计算机的应用,中国石油大学(北京) 油气储运工程专业介绍
  4. linux man命令无效,Linux man命令的具体使用
  5. 采用的php cms分校站点 打开特别慢,phpcms v9 打开网站特别慢 增加数据库缓存方法...
  6. Spring框架中bean的生命周期
  7. 007_CSS ID选择器
  8. html中双重id标签怎么写,PHP读取HTML并处理双重ID外观
  9. java序列化的作用
  10. Android各大热补丁方案分析和比较