LeetCode Perfect Squares
原题链接在这里:https://leetcode.com/problems/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
.
题解:
Let dp[i] denotes the least number of perfect squares sum to i.
Then for all the candiates smaller than i, if the difference between i and candidate is perfect square, then update the dp[candidate]+1. Maintain the smallest.
Thne how to make sure the difference is perfect square.
Let candidate = i - j*j.
Time Complexity: O(nlogn).
Space: O(n).
AC Java:
class Solution {
public int numSquares(int n) {
int [] dp = new int[n+1];
for(int i = 1; i<=n; i++){
dp[i] = i;
for(int j = 0; i-j*j>=0; j++){
dp[i] = Math.min(dp[i], dp[i-j*j]+1);
}
} return dp[n];
}
}
Could also do it from head to tail.
初始化i*i的位置为1, 然后对i + j*j 更新min(dp[i+j*j], dp[i] + 1).
Time Complexity: O(nlogn). Space: O(n).
AC Java:
public class Solution {
public int numSquares(int n) {
if(n < 0){
return 0;
}
int [] dp = new int[n+1];
Arrays.fill(dp, Integer.MAX_VALUE);
for(int i = 0; i*i <= n; i++){
dp[i*i] = 1;
}
for(int i = 1; i<=n; i++){
for(int j = 1; i+j*j<=n; j++){
dp[i+j*j] = Math.min(dp[i+j*j], dp[i]+1);
}
}
return dp[n];
}
}
与Count Primes类似.
LeetCode Perfect Squares的更多相关文章
- [LeetCode] Perfect Squares 完全平方数
Given a positive integer n, find the least number of perfect square numbers (for example, 1, 4, 9, 1 ...
- 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 ...
- 花式求解 LeetCode 279题-Perfect Squares
原文地址 https://www.jianshu.com/p/2925f4d7511b 迫于就业的压力,不得不先放下 iOS 开发的学习,开始走上漫漫刷题路. 今天我想聊聊 LeetCode 上的第2 ...
- [LeetCode] 0279. Perfect Squares 完全平方数
题目 Given a positive integer n, find the least number of perfect square numbers (for example, 1, 4, 9 ...
- [LintCode] Perfect Squares 完全平方数
Given a positive integer n, find the least number of perfect square numbers (for example, 1, 4, 9, 1 ...
- 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个数 选出一些数 他们的乘积是全然平方数 求有多少种方案 思路:每一个数分解因子 每隔 ...
随机推荐
- 基于Berkeley DB实现的持久化队列
转自:http://guoyunsky.iteye.com/blog/1169912 队列很常见,但大部分的队列是将数据放入到内存.如果数据过多,就有内存溢出危险,而且长久占据着内存,也会影响性能.比 ...
- ajax无刷新获取php后台数据
$.ajax({ url:"result.php", //data:{"page":i}, dataType:"json", beforeS ...
- log4net配置文件设置
windows服务执行cmd命令 最长公共子字符串 log4net配置文件设置 2011-11-16 13:15:41| 分类: Notes | 标签: |字号大中小 订阅 log4net ...
- filesize返回的是以字节为单位的数据也可以使之处理数字
1.文件的属性信息获取 首先文件具有类型,在linux下边,有block(块设备,如磁盘分区.CD-ROM).char(以字符为输入的设备,如键盘.打印机).dir(目录类型,目录也是文件的一种).f ...
- Apache是目前应用最广的Web服务器,PHP3是一种类似ASP的脚本语言
一.如何获得软件? 获得这3个软件包的方法很多,目前大多数Linux分发都捆绑了这3个软件包,如RedHat.本文介绍的安装方法是基于从这些软件的官方站点上下载获得的软件包进行的,针对RedHat L ...
- $.each(),$.map()归纳
//$.each()对字典(没有索引).数组(有索引) 遍历 //两个参数 var json={"name":"李可","age":&quo ...
- thinkphp模型层Model、Logic、Service讲解
thinkphp模型层Model.Logic.Service讲解 时间:2014-08-24 15:54:56 编辑:一切随缘 文章来源:php教程网 已阅读:771 次 js特效 ...
- Windows2008 R2修改3389端口教程
默认的3389端口有必要修改么,这个端口,我建议是修改一下为好,最好改大一点,这样安全性会高一点,但然面对高手级攻击,就没有用了,通常高手级过NMAP扫描一个你的IP地址,端便全然显现出来了,好了,下 ...
- BAT批处理(一)
本文摘自博文<BAT批处理文件教程> 这是一篇技术教程,我会用很简单的文字表达清楚自己的意思,只要你识字就能看懂,就能学到知识.写这篇教程的目的,是让每一个看过这些文字的朋友记住一句话:如 ...
- TCPdump抓包命令详解--摘
http://blog.csdn.net/s_k_yliu/article/details/6665673/ http://starsliao.blog.163.com/blog/static/890 ...