Skip to content
体验新版
项目
组织
正在加载...
登录
切换导航
打开侧边栏
爱吃血肠
leetcode
提交
5ba05c23
L
leetcode
项目概览
爱吃血肠
/
leetcode
通知
1
Star
0
Fork
0
代码
文件
提交
分支
Tags
贡献者
分支图
Diff
Issue
0
列表
看板
标记
里程碑
合并请求
0
Wiki
0
Wiki
分析
仓库
DevOps
项目成员
Pages
L
leetcode
项目概览
项目概览
详情
发布
仓库
仓库
文件
提交
分支
标签
贡献者
分支图
比较
Issue
0
Issue
0
列表
看板
标记
里程碑
合并请求
0
合并请求
0
Pages
分析
分析
仓库分析
DevOps
Wiki
0
Wiki
成员
成员
收起侧边栏
关闭侧边栏
动态
分支图
创建新Issue
提交
Issue看板
体验新版 GitCode,发现更多精彩内容 >>
提交
5ba05c23
编写于
3月 27, 2019
作者:
L
luzhipeng
浏览文件
操作
浏览文件
下载
电子邮件补丁
差异文件
279.perfect-squares
上级
d40e8d5b
变更
3
隐藏空白更改
内联
并排
Showing
3 changed file
with
105 addition
and
38 deletion
+105
-38
279.perfect-squares.js
279.perfect-squares.js
+0
-38
279.perfect-squares.md
279.perfect-squares.md
+104
-0
README.md
README.md
+1
-0
未找到文件。
279.perfect-squares.js
已删除
100644 → 0
浏览文件 @
d40e8d5b
/*
* @lc app=leetcode id=279 lang=javascript
*
* [279] Perfect Squares
*
* https://leetcode.com/problems/perfect-squares/description/
*
* algorithms
* Medium (40.98%)
* Total Accepted: 168.2K
* Total Submissions: 408.5K
* Testcase Example: '12'
*
* 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.
*/
/**
* @param {number} n
* @return {number}
*/
var
numSquares
=
function
(
n
)
{
};
279.perfect-squares.md
0 → 100644
浏览文件 @
5ba05c23
## 题目地址
https://leetcode.com/problems/perfect-squares/description/
## 题目描述
```
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.
```
## 思路
直接递归处理即可,但是这种暴力的解法很容易超时。如果你把递归的过程化成一棵树的话(其实就是递归树),
可以看出中间有很多重复的计算。
如果能将重复的计算缓存下来,说不定能够解决时间复杂度太高的问题。
> 递归对内存的要求也很高, 如果数字非常大,也会面临爆栈的风险,将递归转化为循环可以解决。
如果使用DP,其实本质上和递归 + 缓存 差不多。
## 关键点解析
-
如果用递归 + 缓存, 缓存的设计很重要
我的做法是key就是n,value是以n为起点,到达底端的深度。
下次取出缓存的时候用当前的level + 存的深度 就是我们想要的level.
## 代码
```
js
/*
* @lc app=leetcode id=279 lang=javascript
*
* [279] Perfect Squares
*
* https://leetcode.com/problems/perfect-squares/description/
*
* algorithms
* Medium (40.98%)
* Total Accepted: 168.2K
* Total Submissions: 408.5K
* Testcase Example: '12'
*
* 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.
*/
const
mapper
=
{};
function
d
(
n
,
level
)
{
if
(
n
===
0
)
return
level
;
let
i
=
1
;
const
arr
=
[];
while
(
n
-
i
*
i
>=
0
)
{
const
hit
=
mapper
[
n
-
i
*
i
];
if
(
hit
)
{
arr
.
push
(
hit
+
level
);
}
else
{
const
depth
=
d
(
n
-
i
*
i
,
level
+
1
)
-
level
;
mapper
[
n
-
i
*
i
]
=
depth
;
arr
.
push
(
depth
+
level
);
}
i
++
;
}
return
Math
.
min
(...
arr
);
}
/**
* @param {number} n
* @return {number}
*/
var
numSquares
=
function
(
n
)
{
return
d
(
n
,
0
);
};
```
README.md
浏览文件 @
5ba05c23
...
...
@@ -30,6 +30,7 @@ leetcode题解,记录自己的leecode解题之路。
-
[
328.odd-even-linked-list
](
./328.odd-even-linked-list.md
)
-
[
445.add-two-numbers-ii
](
./445.add-two-numbers-ii.md
)
-
[
877.stone-game
](
./877.stone-game.md
)
-
[
279.perfect-squares
](
./279.perfect-squares.md
)
### 困难难度
-
[
145.binary-tree-postorder-traversal
](
./145.binary-tree-postorder-traversal.md
)
...
...
编辑
预览
Markdown
is supported
0%
请重试
或
添加新附件
.
添加附件
取消
You are about to add
0
people
to the discussion. Proceed with caution.
先完成此消息的编辑!
取消
想要评论请
注册
或
登录