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的更多相关文章

  1. [LeetCode] Valid Perfect Square 检验完全平方数

    Given a positive integer num, write a function which returns True if num is a perfect square else Fa ...

  2. [leetcode]367. Valid Perfect Square验证完全平方数

    Given a positive integer num, write a function which returns True if num is a perfect square else Fa ...

  3. Leetcode之二分法专题-367. 有效的完全平方数(Valid Perfect Square)

    Leetcode之二分法专题-367. 有效的完全平方数(Valid Perfect Square) 给定一个正整数 num,编写一个函数,如果 num 是一个完全平方数,则返回 True,否则返回 ...

  4. [LeetCode] 367. Valid Perfect Square 检验完全平方数

    Given a positive integer num, write a function which returns True if num is a perfect square else Fa ...

  5. 【LeetCode】367. Valid Perfect Square 解题报告(Java & Python)

    作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 方法一:完全平方式性质 方法二:暴力求解 方法三:二 ...

  6. Leetcode 367. Valid Perfect Square

    Given a positive integer num, write a function which returns True if num is a perfect square else Fa ...

  7. 【leetcode】367. Valid Perfect Square

    题目描述: Given a positive integer num, write a function which returns True if num is a perfect square e ...

  8. C#LeetCode刷题之#367-有效的完全平方数(Valid Perfect Square)

    问题 该文章的最新版本已迁移至个人博客[比特飞],单击链接 https://www.byteflying.com/archives/3869 访问. 给定一个正整数 num,编写一个函数,如果 num ...

  9. [LeetCode] Perfect Squares 完全平方数

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

随机推荐

  1. 用Docker封装一个web应用(Django)

    一.复用以前一个封装了SSH的镜像,如果没有封装SSH,可以使用自己的镜像或参考我以前博文:叫板OpenStack:用Docker实现私有云 的前五步 接下来便是正题. 二.部署过程 1.查看镜像 R ...

  2. javascript判断手机旋转横屏竖屏

    javascript判断手机旋转横屏竖屏 // 横屏竖屏函数 function orientationChange(){ switch(window.orientation) { case 0: // ...

  3. C语言:关于socket的基础知识点

    /** * ---结构体--- * * #include <sys/socket.h> * struct sockaddr { * unsigned short sa_family; * ...

  4. 条件变量pthread_cond_t怎么用

    #include <pthread.h> #include <stdio.h> #include <stdlib.h> pthread_mutex_t mutex ...

  5. Collections和Arrays常用方法

    Collections:常见方法: 1, 对list进行二分查找: 前提该集合一定要有序. int binarySearch(list,key); //必须根据元素自然顺序对列表进行升级排序 //要求 ...

  6. SQL基础之数据库快照

    1.认识快照 如名字一样,数据库快照就可以理解为数据库某一时刻的照片,它记录了此时数据库的数据信息.如果要认识快照的本质,那就要了解快照的工作原理.当我们执行t-sql创建快照后,此时就会创建一个或多 ...

  7. 如何采集QQ群中所有成员QQ号码

    安装Google Chrome浏览器 安装Google插件:Regex Scraper 在群成员页面点击Regex 插件, 粘贴上这个代码 text_overflow">([\S\s] ...

  8. Entity Framework与ADO.Net及NHibernate的比较

    Entity Framework  是微软推荐出.NET平台ORM开发组件, EF相对于ado.net 的优点 (1)开发效率高,Entity Framework的优势就是拥有更好的LINQ提供程序. ...

  9. Linux C中结构体初始化

          在阅读GNU/Linux内核代码时,我们会遇到一种特殊的结构初始化方式.该方式是某些C教材(如谭二版.K&R二版)中没有介绍过的.这种方式称为指定初始化(designated in ...

  10. mysql忘记密码怎么办?

    mysql有时候忘记密码了怎么办?我给出案例和说明!一下就解决了! Windows下的实际操作如下 1.关闭正在运行的MySQL. 2.打开DOS窗口,转到mysql\bin目录. 3.输入mysql ...