[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 解释 ...
随机推荐
- HDU - 4462 Scaring the Birds
It's harvest season now! Farmer John plants a lot of corn. There are many birds living around his co ...
- HDU 3949 XOR (线性基第k小)题解
题意: 给出\(n\)个数,求出子集异或第\(k\)小的值,不存在输出-1. 思路: 先用线性基存所有的子集,然后对线性基每一位进行消元,保证只有\(d[i]\)的\(i\)位存在1,那么这样变成了一 ...
- git 取消未成功的 merge
git 取消未成功的 merge # 合并时遇到冲突想取消操作,恢复index $ git merge --abort # 可以回退到某个提交 $ git reset --hard # 可以撤销某个提 ...
- ES6 Map All In One
ES6 Map All In One Map 字典/地图 Set 集合 https://developer.mozilla.org/en-US/docs/Web/JavaScript/Referenc ...
- javascript nested object merge
javascript nested object merge deep copy Object reference type function namespace(oNamespace, sPacka ...
- React Styleguidist
React Styleguidist https://www.thoughtworks.com/cn/radar/techniques/micro-frontends https://github.c ...
- vue-eahars生产编译报错
{ test: /\.js$/, loader: 'babel-loader', include: [resolve('src'), resolve('test'), resolve('node_mo ...
- 我ssh框架遇到报错及处理方式
Exception encountered during context initialization - cancelling refresh attempt 修改hbm.xml后遇到的问题,错误可 ...
- ForkJoinPool大型图文现场(一阅到底 vs 直接收藏)
知识回顾 并发工具类我们已经讲了很多,这些工具类的「目标」是让我们只关注任务本身,并且忽视线程间合作细节,简化了并发编程难度的同时,也增加了很多安全性.工具类的对使用者的「目标」虽然一致,但每一个工具 ...
- mui调用本地相册调用相机上传照片
调用mui的常用库和jquery html部分: <header class="mui-bar mui-bar-nav"> <a class="mui- ...