Leetcode Perfect Square
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.
这道题首先想到的算法是DP。每个perfect square number对应的解都是1。先生成一个n+1长的DP list。对于每个i,可以用dp[i] = min(dp[j] + dp[i-j], dp[i])来更新,这里j 是<= i 的一个perfect square number。但是DP的算法超时。
class Solution(object):
def numSquares(self, n):
"""
:type n: int
:rtype: int
"""
MAX = 2147483647
m = 1
perfect = [m]
while m**2 <= n:
perfect.append(m**2)
m += 1 dp = [MAX]*(n+1)
dp[0] = 1
for x in perfect:
dp[x] = 1 for i in range(2, n+1):
curP = [x for x in perfect if x <= i]
for j in curP:
dp[i] = min(dp[j] + dp[i-j], dp[i]) return dp[-1]
解法二: 来自 https://www.cnblogs.com/grandyang/p/4800552.html
任何一个正整数都可以写成最多4个数的平方和。然后又两条性质可以简化题目:
1. 4q 和 q 的答案一样,i.e. 3, 12。
2. 如果一个数除以8余7 <=> 答案是4。
class Solution(object):
def numSquares(self, n):
"""
:type n: int
:rtype: int
"""
while n % 4 == 0:
n = n//4 if n % 8 == 7:
return 4 a = 0
while a**2 <= n:
b = int(math.sqrt(n - a**2))
if a**2 + b**2 == n:
if a>0 and b>0:
return 2
if a == 0 and b>0:
return 1
if a>0 and b==0:
return 1
a += 1
return 3
Leetcode Perfect Square的更多相关文章
- [LeetCode] Valid Perfect Square 检验完全平方数
Given a positive integer num, write a function which returns True if num is a perfect square else Fa ...
- [leetcode]367. Valid Perfect Square验证完全平方数
Given a positive integer num, write a function which returns True if num is a perfect square else Fa ...
- Leetcode之二分法专题-367. 有效的完全平方数(Valid Perfect Square)
Leetcode之二分法专题-367. 有效的完全平方数(Valid Perfect Square) 给定一个正整数 num,编写一个函数,如果 num 是一个完全平方数,则返回 True,否则返回 ...
- [LeetCode] 367. Valid Perfect Square 检验完全平方数
Given a positive integer num, write a function which returns True if num is a perfect square else Fa ...
- 【LeetCode】367. Valid Perfect Square 解题报告(Java & Python)
作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 方法一:完全平方式性质 方法二:暴力求解 方法三:二 ...
- Leetcode 367. Valid Perfect Square
Given a positive integer num, write a function which returns True if num is a perfect square else Fa ...
- 【leetcode】367. Valid Perfect Square
题目描述: Given a positive integer num, write a function which returns True if num is a perfect square e ...
- C#LeetCode刷题之#367-有效的完全平方数(Valid Perfect Square)
问题 该文章的最新版本已迁移至个人博客[比特飞],单击链接 https://www.byteflying.com/archives/3869 访问. 给定一个正整数 num,编写一个函数,如果 num ...
- [LeetCode] Perfect Squares 完全平方数
Given a positive integer n, find the least number of perfect square numbers (for example, 1, 4, 9, 1 ...
随机推荐
- Python的高级特性9:蹩脚的多态
学习了java再来看python的多态,总感觉怪怪的,很蹩脚.. 1.python的父类根本不能调用子类的方法,只能蹩脚的依靠重写方法,然后在运行时去调用,实现伪多态... 2.所谓的鸭子类型看起来很 ...
- div+css兼容 ie6_ie7_ie8_ie9_ie10和FireFox_Chrome等浏览器方法
1.div的垂直居中问题 vertical-align:middle; 将行距增加到和整个DIV一样高 line-height:200px; 然后插入文字,就垂直居中了.缺点是要控制内容不要换行 ...
- noi题库(noi.openjudge.cn) 1.9编程基础之顺序查找T06——T15
T06 笨小猴 描述 笨小猴的词汇量很小,所以每次做英语选择题的时候都很头疼.但是他找到了一种方法,经试验证明,用这种方法去选择选项的时候选对的几率非常大! 这种方法的具体描述如下:假设maxn是单词 ...
- Nginx文件类型错误解析漏洞--攻击演练
今天看书看到其中提到的一个漏洞,那就是Nginx+PHP的服务器中,如果PHP的配置里 cgi.fix_pathinfo=1 那么就会产生一个漏洞.这个配置默认是1的,设为0会导致很多MVC框架(如T ...
- idea配置。
1.project Structure — 修改project(1.name,sdk,level(6-@Override in interface)) 修改modules(点击web,加上source ...
- Vue学习笔记-1
前言 本文不是Vue.js的教程,只是一边看官网Vue的教程文档一边记录并总结学习过程中遇到的一些问题和思考的笔记. 1.vue和avalon一样,都不支持VM初始时不存在的属性 而在Angular里 ...
- JavaScript UI选型及Jquery EasyUI使用经验谈
最近由于项目需要,对js UI作了一些简单的了解和使用,有自己的一些想法,在这里留个记录. 当然,我的专注点在管理系统的范围内,所以互联网网站及其他形态的应用这里不提及,所以jQuery UI和Boo ...
- 【python】 [基础] 数据类型,字符串和编码
python笔记,写在前面:python区分大小写1.科学计数法,把10用e代替,1.23x10·9就是 1.23e9 或者 0.00012就是1 ...
- JS 获取上一层目录
派生到我的代码片 <script type="text/javascript"> //返回当前工作目录 function GetCurrDir(){ var pathN ...
- Android activity跳转方式
方法一:通过SetContentView切换Layout来实现界面的切换,这种方法相当于重绘Activity. protected void onCreate(Bundle savedInstance ...