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的算法超时。

 1 class Solution(object):
 2     def numSquares(self, n):
 3         """
 4         :type n: int
 5         :rtype: int
 6         """
 7         MAX = 2147483647
 8         m = 1
 9         perfect = [m]
10         while m**2 <= n:
11             perfect.append(m**2)
12             m += 1
13
14         dp = [MAX]*(n+1)
15         dp[0] = 1
16         for x in perfect:
17             dp[x] = 1
18
19         for i in range(2, n+1):
20             curP = [x for x in perfect if x <= i]
21             for j in curP:
22                 dp[i] = min(dp[j] + dp[i-j], dp[i])
23
24         return dp[-1]

解法二: 来自 https://www.cnblogs.com/grandyang/p/4800552.html

任何一个正整数都可以写成最多4个数的平方和。然后又两条性质可以简化题目:

1. 4q 和 q 的答案一样,i.e. 3, 12。

2. 如果一个数除以8余7 <=> 答案是4。

 1 class Solution(object):
 2     def numSquares(self, n):
 3         """
 4         :type n: int
 5         :rtype: int
 6         """
 7         while n % 4 == 0:
 8             n = n//4
 9
10         if n % 8 == 7:
11             return 4
12
13         a = 0
14         while a**2 <= n:
15             b = int(math.sqrt(n - a**2))
16             if a**2 + b**2 == n:
17                 if a>0 and b>0:
18                     return 2
19                 if a == 0 and b>0:
20                     return 1
21                 if a>0 and b==0:
22                     return 1
23             a += 1
24         return 3

转载于:https://www.cnblogs.com/lettuan/p/6183123.html

Leetcode 279. Perfect Square相关推荐

  1. leetcode -- 279. Perfect Squares

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

  2. LeetCode 279. Perfect Squares

    279. Perfect Squares Given a positive integer n, find the least number of perfect square numbers (fo ...

  3. leetcode 279. Perfect Squares | 279. 完全平方数(动态规划,Java)

    题目 https://leetcode.com/problems/perfect-squares/ 题解:动态规划 参考:[宫水三叶]详解完全背包一维空间优化推导(附背包问题攻略) 首先初始化长度为 ...

  4. LeetCode Valid Perfect Square(是否是平方数)

    题意:给出一正整数,不用 sqrt函数,判断该数是否是平方数 思路: 第一种,连续奇数和为平方数 代码如下: public class Solution {public boolean isPerfe ...

  5. [leetcode] 367. Valid Perfect Square

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

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

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

  7. 367. Valid Perfect Square

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

  8. Leetcode 279. 完全平方数

    Leetcode 279. 完全平方数 1.问题分析 2.问题解决 3.总结 1.问题分析 题目链接:https://leetcode-cn.com/problems/perfect-squares/ ...

  9. 279. Perfect Squares

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

最新文章

  1. LeetCode简单题之拼写单词
  2. linxu命令之cp 拷贝整个目录下的所有文件
  3. Ubuntu 16.04设置IP、网关、DNS
  4. spark面试总结1
  5. 用友BQ商业智能平台——图表功能
  6. Oracle segment啥意思,关于oracle数据库段segment的小结
  7. 加州大学黑科技:激光使电子设备不再依赖半导体材料
  8. JavaScript数据结构与算法(六) 链表的实现
  9. CListCtrl的使用
  10. Leetcode 266.回文排列
  11. 我的航拍直升机 控制基站软件的编写历程(2.1)
  12. 今日金融词汇---股价复权,是什么?
  13. VSCode的VUE项目侧边栏打开资源管理器中的NPM脚本
  14. Triplet-Graph Reasoning Network for few-shot Metal Generic Surface Defect Segmentation论文理解
  15. 为什么世界上只有13个根域名服务器
  16. 【直播教程】直播间没人看?5大技巧教你提升!
  17. 什么是Java SE、Java EE、Java ME?
  18. 地图可视化“一网打尽“
  19. 高频故障-文件扩展名消失(windows)
  20. notepad++格式化XML

热门文章

  1. python js返回 json_[python爬虫]把js转化成json
  2. JMeter - 如何创建可重用和模块化测试脚本
  3. module.exports和exports得区别
  4. 关于Trie的一些算法
  5. 栈和队列的区别,栈和堆的区别
  6. SurvivalShooter学习笔记(八.敌人管理器)
  7. mysql outfile csv_sql-MySQL导出到outfile:CSV转义字符
  8. Spring Boot 项目瘦身指南,瘦到不可思议!129M->1.3M
  9. 微信支付宝扫一扫进入小程序的相关配置
  10. 局域网抢答器_基于童芯派的抢答器V1.0