[LeetCode 279.] Perfect Squres
LeetCode 279. Perfect Squres
DP 是笨办法中的高效办法,又是一道可以被好办法打败的 DP 题。
题目描述
Given a positive integer n, find the least number of perfect square numbers (for example, 1, 4, 9, 16, ...) which sum to n.
Example 1:
Input: n = 12
Output: 3
Explanation: 12 = 4 + 4 + 4.
Example 2:
Input: n = 13
Output: 2
Explanation: 13 = 4 + 9.
解题思路
这道题是说,给出一个正整数 n,问可以最少用几个完全平方数的加和来表示。
我的第一个思路是 n = a + b (a <b) 然后用 DP 来消除重复计算,结果超时了,因为时间复杂度太高,到了 O(N^2) 级别。
另一个思路好一点,拆成 n = a + k*k 的形式,同样的 DP 算法,时间复杂度只有 O(NlogN)。
其实本题最佳算法可以达到 O(logN),用到了 Lagrange 四平方定理: 任何一个正整数都可以表示成不超过四个整数的平方之和。这里贴出来源和代码,仅作了解。
参考代码
/*
* @lc app=leetcode id=279 lang=cpp
*
* [279] Perfect Squares
*/
// @lc code=start
class Solution {
public:
/*
int numSquares(int n) {
vector<int> dp(n+1);
dp[1] = 1;
for (int k=2; k<=n; k++) {
int x = sqrt(k);
if (x * x == k) {
dp[k] = 1;
} else {
int res = k; // 1 + 1 + 1 + ...
for (int i = 1; i <= k/2; i++) {
res = min(res, dp[i]+dp[k-i]);
} // split into any a+b
dp[k] = res;
}
}
return dp[n];
} // O(N^2), TLE, 585/588 cases passed
*/
int numSquares(int n) {
vector<int> dp(n+1);
dp[0] = 0;
for (int k=1; k<=n; k++) {
int x = sqrt(k);
if (x * x == k) {
dp[k] = 1;
} else {
int res = k; // 1 + 1 + 1 + ...
for (int i=1; i<=x; i++) {
res = min(res, 1 + dp[k-i*i]);
} // split into i*i+b
dp[k] = res;
}
}
return dp[n];
} // O(NlogN), AC
};
// @lc code=end
O(logN) 数学解法
参考博客 grandyang
前两行代码对算法效率的提升很大,虽然不知道怎么证明这个 ……
class Solution {
public:
int numSquares(int n) {
while (n % 4 == 0) n /= 4;
if (n % 8 == 7) return 4;
for (int a = 0; a * a <= n; ++a) {
int b = sqrt(n - a * a);
if (a * a + b * b == n) {
return !!a + !!b;
}
}
return 3;
}
};
[LeetCode 279.] Perfect Squres的更多相关文章
- leetcode@ [279]Perfect Squares
https://leetcode.com/problems/perfect-squares/ Given a positive integer n, find the least number of ...
- [LeetCode] 279. Perfect Squares 完全平方数
Given a positive integer n, find the least number of perfect square numbers (for example, 1, 4, 9, 1 ...
- (BFS) leetcode 279. 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 (medium)
原题链接 题意: 给一个非整数,算出其最少可以由几个完全平方数组成(1,4,9,16……) 思路: 可以得到一个状态转移方程 dp[i] = min(dp[i], dp[i - j * j] + ) ...
- LeetCode 279. 完全平方数(Perfect Squares) 7
279. 完全平方数 279. Perfect Squares 题目描述 给定正整数 n,找到若干个完全平方数(比如 1, 4, 9, 16, ...)使得它们的和等于 n.你需要让组成和的完全平方数 ...
- 【LeetCode】279. Perfect Squares 解题报告(C++ & Java)
作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 四平方和定理 动态规划 日期 题目地址:https: ...
- 花式求解 LeetCode 279题-Perfect Squares
原文地址 https://www.jianshu.com/p/2925f4d7511b 迫于就业的压力,不得不先放下 iOS 开发的学习,开始走上漫漫刷题路. 今天我想聊聊 LeetCode 上的第2 ...
- 【leetcode】Perfect Squares (#279)
Given a positive integer n, find the least number of perfect square numbers (for example, 1, 4, 9, 1 ...
- LeetCode 279. 完全平方数(Perfect Squares)
题目描述 给定正整数 n,找到若干个完全平方数(比如 1, 4, 9, 16, ...)使得它们的和等于 n.你需要让组成和的完全平方数的个数最少. 示例 1: 输入: n = 12 输出: 3 解释 ...
随机推荐
- K8S(02)管理核心资源的三种基本方法
系列文章说明 本系列文章,可以基本算是 老男孩2019年王硕的K8S周末班课程 笔记,根据视频来看本笔记最好,否则有些地方会看不明白 需要视频可以联系我 管理k8s核心资源的三种基本方法: 目录 系列 ...
- System.Windows.Forms.Help
在开发过程中,基本都需要实现帮助功能,而一般帮助功能就是打开一个帮助文档,System.Windows.Forms提供了Help类用于打开帮助文档,挺方便的. Help类提供的方法如下: Name ...
- 深入理解gradle中的task
目录 简介 定义task tasks 集合类 Task 之间的依赖 定义task之间的顺序 给task一些描述 task的条件执行 task rule Finalizer tasks 总结 深入理解g ...
- Linux 驱动框架---dm9000分析
前面学习了下Linux下的网络设备驱动程序的框架inux 驱动框架---net驱动框架,感觉知道了一个机器的大致结构还是不太清楚具体的细节处是怎么处理的,所以今天就来以dm9000这个网上教程最多的驱 ...
- 010. NET5_命令参数读取+配置多种读取
上节课遗留问题:上节脚本启动后,CSS样式丢失问题 解决办法:a.拷贝丢失的wwwroot目录:b. 给UesStaticFiles类指定读取wwwroot目录 静态文件读取 Nuget引入:Micr ...
- Ubuntu 18.04 + pip3 install virtualenvwrapper 找不到virtualenvwrapper.sh
Reference Ubuntu 18.04 只自带python3.6.5, 因此不想装python2了, 但通过apt install 装virtualenvwrapper时发现必须得装python ...
- Adobe DreamWeaver CC 快捷键
1 1 ADOBE DREAMWEAVER CC Shortcuts: DREAMWEAVER CC DOCUMENT EDITING SHORTCUTS Select Dreamweaver > ...
- YouTube 视频下载工具
YouTube 视频下载工具 我不生产视频,只是优秀视频的搬运工! YouTube-dl https://github.com/search?q=youtube-dl https://github.c ...
- Linux bash shell All In One
Linux bash shell All In One Linux https://tinylab.gitbooks.io/shellbook/content/zh/chapters/01-chapt ...
- node.js delete directory & file system
node.js delete directory & file system delete a not empty directory https://nodejs.org/api/fs.ht ...