[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 解释 ...
随机推荐
- PAT L2-004. 这是二叉搜索树吗?【前序遍历转化为后序遍历】
一棵二叉搜索树可被递归地定义为具有下列性质的二叉树:对于任一结点, 其左子树中所有结点的键值小于该结点的键值: 其右子树中所有结点的键值大于等于该结点的键值: 其左右子树都是二叉搜索树. 所谓二叉搜索 ...
- codeforces 8B
B. Obsession with Robots time limit per test 2 seconds memory limit per test 64 megabytes input stan ...
- 微软大楼设计方案(中等) 推公式+RMQ问题
近日,微软新大楼的设计方案正在广泛征集中,其中一种方案格外引人注目.在这个方案中,大楼由 nn 栋楼组成,这些楼从左至右连成一排,编号依次为 11 到 nn,其中第 ii 栋楼有 h_ihi层. ...
- 蓝湖 UI 设计稿上如何生成渐变色和复制渐变色
蓝湖 UI 设计稿上如何生成渐变色和复制渐变色 Sketch 生成渐变色 不要上传图片,切图 如果是切图,切图模式下就不会生成 css 代码了 复制渐变色 OK .button { width: 28 ...
- hardsource bug
hardsource bug webpack crashed bug memory stackoverflow [hardsource:32210703] Could not freeze refs ...
- node.js 中间件
node.js 中间件 node.js middleware Express middleware body-parser cookie-parser cookie-session cors csur ...
- 视屏剪辑软件 & free video editor
视屏剪辑软件 & free video editor purpose add animation keyframe to tutorials video vlog demos tutorial ...
- bob and brad physical therapy knee exercise
bob and brad physical therapy knee exercise 鲍勃和布拉德物理治疗膝关节运动 https://bobandbrad.com/ youtube https:// ...
- Chrome DevTools & Slow 3G Network
Chrome DevTools & Slow 3G Network shortcuts https://developers.google.com/web/tools/chrome-devto ...
- shit 环信 IM SDK & IM SDK & web
shit 环信 IM SDK & IM SDK & web 环信 IM SDK, 采坑大全 自己写 UI appkey 是否正确 password 是字符串,不是 数字 HTTPS 是 ...