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个数 选出一些数 他们的乘积是全然平方数 求有多少种方案 思路:每一个数分解因子 每隔 ...
随机推荐
- Linux下MySQL的备份与还原
Linux下MySQL的备份与还原 1. 备份 [root@localhost ~]# cd /var/lib/mysql (进入到MySQL库目录,根据自己的MySQL的安装情况调整目录) [roo ...
- github配置
注册github账号: 准备秘钥文件: 认证: https://github.com 测试秘钥: 创建仓库: 执行下面命令创建git远程仓库: 添加一个two.txt文件:
- java语言中一些使用的小技巧(区别于c++)
正在自学java中...想记录下java和c++在一些小的方面的不同点.(未完待续...) java中class的对象均是引用类型的,如果想把连个同类型的对象相关联起来,只要将一个赋值给另一个就可以了 ...
- android之Chronometer
首先定义activity_main.xml文件 代码如下: <LinearLayout xmlns:android="http://schemas.android.com/apk/re ...
- WBS练习
我们把这次团队程序设计分成了6个模块,让每一个同学都能参与其中,然后让每一个人选一个自己喜欢的模块,最后数据库设计这个部分就大家一起来做. Everybody's task allocation is ...
- 分布式架构高可用架构篇_04_Keepalived+Nginx实现高可用Web负载均衡
参考: 龙果学院http://www.roncoo.com/share.html?hamc=hLPG8QsaaWVOl2Z76wpJHp3JBbZZF%2Bywm5vEfPp9LbLkAjAnB%2B ...
- 【IOS笔记】Creating Custom Content View Controllers
Creating Custom Content View Controllers 自定义内容视图控制器 Custom content view controllers are the heart of ...
- 树莓派系统安装、HDMI显示
树莓派上可以安装多种操作系统,我们采用的是官方的基于debian的raspbian.系统安装方式见 安装完系统由于我使用的是7寸的HDMI屏,装完系统如果配置不该的话会导致右边有一部分无法显示,所以在 ...
- mysql默认字符集修改
(1) 最简单的修改方法,就是修改mysql的my.ini文件中的字符集键值,添加 [mysql] default-character-set = utf8 [mysqld] character_se ...
- 在Delphi中如何控制其它应用程序窗口
在编写Delphi的应用程序中,常常涉及对其它Windows应用程序的操作.例如,在数据库的管理系统中,财务人员需要使用计算器,即可调用Windows内含的计算器功能,若每次使用,均通过“开始/程序/ ...