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

  1. leetcode@ [279]Perfect Squares

    https://leetcode.com/problems/perfect-squares/ Given a positive integer n, find the least number of ...

  2. [LeetCode] 279. Perfect Squares 完全平方数

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

  3. (BFS) leetcode 279. Perfect Squares

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

  4. [leetcode] #279 Perfect Squares (medium)

    原题链接 题意: 给一个非整数,算出其最少可以由几个完全平方数组成(1,4,9,16……) 思路: 可以得到一个状态转移方程  dp[i] = min(dp[i], dp[i - j * j] + ) ...

  5. LeetCode 279. 完全平方数(Perfect Squares) 7

    279. 完全平方数 279. Perfect Squares 题目描述 给定正整数 n,找到若干个完全平方数(比如 1, 4, 9, 16, ...)使得它们的和等于 n.你需要让组成和的完全平方数 ...

  6. 【LeetCode】279. Perfect Squares 解题报告(C++ & Java)

    作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 四平方和定理 动态规划 日期 题目地址:https: ...

  7. 花式求解 LeetCode 279题-Perfect Squares

    原文地址 https://www.jianshu.com/p/2925f4d7511b 迫于就业的压力,不得不先放下 iOS 开发的学习,开始走上漫漫刷题路. 今天我想聊聊 LeetCode 上的第2 ...

  8. 【leetcode】Perfect Squares (#279)

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

  9. LeetCode 279. 完全平方数(Perfect Squares)

    题目描述 给定正整数 n,找到若干个完全平方数(比如 1, 4, 9, 16, ...)使得它们的和等于 n.你需要让组成和的完全平方数的个数最少. 示例 1: 输入: n = 12 输出: 3 解释 ...

随机推荐

  1. Vue computed props pass params

    Vue computed props pass params vue 计算属性传参数 // 计算 spreaderAlias spreaderAlias () { console.log('this. ...

  2. Vue dynamic component All In One

    Vue dynamic component All In One Vue 动态组件 vue 2.x https://vuejs.org/v2/guide/components-dynamic-asyn ...

  3. CDN maxcdn for speed up

    https://www.maxcdn.com/ The Next Generation CDN.Plug into a whole new level of speed.From: https://w ...

  4. 如何在 网站页面中插入ppt/pdf 文件,使用插件,Native pdf 支持,chrome,Edge,Firefox,

    1 经过测试:在网页中插入 ppt 不好使:可能是浏览器=>同源策略 error?             pdf 可以正常使用:   <前提:一定要放在服务器端才行!> 2 经过e ...

  5. JavaScript code 性能优化

    1 1 1 JavaScript 性能优化 prototype 闭包 Closure 内存泄漏 event system 1 定义类方法以下是低效的,因为每次构建baz.Bar的实例时,都会为foo创 ...

  6. Flutter 1.17.x

    Flutter 1.17.x Flutter (Channel stable, v1.17.3, on Mac OS X 10.15.5 19F101, locale en-CN) https://f ...

  7. WebAssembly JavaScript APIs

    WebAssembly JavaScript APIs https://developer.mozilla.org/en-US/docs/WebAssembly/Using_the_JavaScrip ...

  8. 新手如何通过内存和NGK DeFi Baccarat进行组合投资?

    区块链市场在2020年迎来了大爆发,资本市场异常火热.无论是内存,还是DeFi,都无疑是这个火爆的区块链市场中的佼佼者.通过投资内存和DeFi,很多投资者都已经获取了非常可观的收益,尝到了资本市场带来 ...

  9. Python算法_两数之和(01)

    给定一个整数数组 nums 和一个目标值 target,请你在该数组中找出和为目标值的那 两个 整数,并返回他们的数组下标. 你可以假设每种输入只会对应一个答案.但是,数组中同一个元素不能使用两遍. ...

  10. apply方法的实现原理

    apply 的核心原理: 将函数设为对象的属性 执行和删除这个函数 指定 this 到函数并传入给定参数执行函数 如果不传参数,默认指向 window Function.prototype.myApp ...