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.

题目大意:给一个正整数,找出可以由最少个平方数构成的数量是多少。

解题思路:dynamic programming,递推公式F[n]=min{F[n-1]+1,F[n-4]+2,F[n-9]+3,...};

public class Solution {
int[] f;
public int numSquares(int n) {
f=new int[n+1];
Arrays.fill(f,Integer.MAX_VALUE);
f[1]=1;
for(int i=2;i<=n;i++){
int r = (int)Math.sqrt(i);
if(i==r*r){
f[i]=1;
continue;
}
for(int j=1;j<=r;j++){
f[i]=Math.min(f[i],f[i-j*j]+1);
}
}
return f[n];
}
}

Perfect Squares——Leetcode的更多相关文章

  1. LeetCode Perfect Squares

    原题链接在这里:https://leetcode.com/problems/perfect-squares/ 题目: Given a positive integer n, find the leas ...

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

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

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

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

  4. 花式求解 LeetCode 279题-Perfect Squares

    原文地址 https://www.jianshu.com/p/2925f4d7511b 迫于就业的压力,不得不先放下 iOS 开发的学习,开始走上漫漫刷题路. 今天我想聊聊 LeetCode 上的第2 ...

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

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

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

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

  7. Perfect Squares

    Perfect Squares Total Accepted: 18854 Total Submissions: 63048 Difficulty: Medium Given a positive i ...

  8. CF914A Perfect Squares

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

  9. Light OJ 1288 Subsets Forming Perfect Squares 高斯消元求矩阵的秩

    题目来源:Light OJ 1288 Subsets Forming Perfect Squares 题意:给你n个数 选出一些数 他们的乘积是全然平方数 求有多少种方案 思路:每一个数分解因子 每隔 ...

随机推荐

  1. 分享一下常用的hosts列表

    #以下的hosts能够保证基本的谷歌服务不受影响,其他网站则不一定能够顺利访问,Youtube视频无法播放,但是可以顺利上传视频 #Thanks smarthosts  ,  老夏 ,Humorce  ...

  2. mysql连接错误:Cannot get hostname for your address

    问题 环境:win7 + 64Bit + 本地mysql5.6 问题:navicat连接本地mysql数据库,提示“Cannot get hostname for your address”,但是连接 ...

  3. C#DbHelperOleDb,Access数据库帮助类 (转载)

    主要功能如下数据访问抽象基础类 主要是访问Access数据库主要实现如下功能 .数据访问基础类(基于OleDb)Access数据库, .得到最大值:是否存在:是否存在(基于OleDbParameter ...

  4. fiddler接口测试

    浏览器中,可直接进行get接口测试:调用post方法的接口测试可用fiddler测试(当然,fiddler也支持get),如下图 [Execute]后双击左侧请求记录记录即可查看响应结果

  5. java - String 浅谈

    /** * String s1 = "a"; * 编译器会先检查常量池中是否已经有"a": * 如果没有,则在常量池先创建,后引用. * 如果有,则直接引用; ...

  6. error MSB6006: “CL.exe”已退出

    解决方案之一: 删除 \Windows\System32 目录下 mspdb110.dll. 试试吧.

  7. linux rman shell

    # make direcory for backset file and scripts file,in my case /backup/db_bak cd   /backup/db_bak mkdi ...

  8. jQuery慢慢啃之事件对象(十一)

    1.event.currentTarget//在事件冒泡阶段中的当前DOM元素 $("p").click(function(event) { alert( event.curren ...

  9. 2、Python djang 框架下的word Excel TXT Image 等文件的下载

    2.python实现文件下载 (1)方法一.直接用a标签的href+数据库中文件地址,即可下载.缺点:word excel是直接弹框下载,对于image txt 等文件的下载方式是直接在新页面打开. ...

  10. Android内的生命周期整理

    1. Android App的生命周期: 2. Application的生命周期: 3. Activity的生命周期: 3.1 Fragment的生命周期: 4. Service的生命周期:5. Br ...