提交 b159f16f 编写于 作者: L luzhipeng

feat: 修改#209 #279, 增加todo

上级 3f8598ef
......@@ -118,9 +118,9 @@ leetcode 题解,记录自己的 leetcode 解题之路。
- [199.binary-tree-right-side-view](./problems/199.binary-tree-right-side-view.md)
- [201.bitwise-and-of-numbers-range](./problems/201.bitwise-and-of-numbers-range.md)
- 🆕 [208.implement-trie-prefix-tree](./problems/208.implement-trie-prefix-tree.md)
- [209.minimum-size-subarray-sum](./problems/209.minimum-size-subarray-sum.md)
- 🖊 [209.minimum-size-subarray-sum](./problems/209.minimum-size-subarray-sum.md)
- [240.search-a-2-d-matrix-ii](./problems/240.search-a-2-d-matrix-ii.md)
- [279.perfect-squares](./problems/279.perfect-squares.md)
- 🖊 [279.perfect-squares](./problems/279.perfect-squares.md)
- [322.coin-change](./problems/322.coin-change.md)
- [328.odd-even-linked-list](./problems/328.odd-even-linked-list.md)
- [416.partition-equal-subset-sum](./problems/416.partition-equal-subset-sum.md)
......@@ -169,9 +169,9 @@ anki - 文件 - 导入 - 下拉格式选择“打包的 anki集合”,然后
- [365.water-and-jug-problem](./todo/365.water-and-jug-problem.js)
- anki 卡片 完善
- [anki 卡片 完善](./assets/anki/)
- 字符串类问题汇总
- [字符串类问题汇总](./todo/str/)
## 交流群
......
<mxfile modified="2019-04-24T03:55:11.597Z" host="www.draw.io" agent="Mozilla/5.0 (Macintosh; Intel Mac OS X 10_12_6) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/73.0.3683.86 Safari/537.36" etag="RuhMS0eC7MZ-eP7ukQ4K" version="10.6.3" type="device"><diagram id="2aZ_QtbOp8G18H4-aLN5" name="第 1 页">7ZpBb5swGIZ/DcdF2AYDxzZttsMqTcqh2mlywAFUwBScJemvnymGBOyknZYQouWU8NkY87yfzWuMgabp5mtB8uiJBTQxoBlsDPRgQAiQ54qfKrKtIw4GdSAs4kBW2gXm8RuVQVNGV3FAy05FzljC47wb9FmWUZ93YqQo2LpbbcmS7lVzElIlMPdJokaf44BHddSFzi7+jcZh1FwZYK8uSUlTWd5JGZGArfdC6NFA04IxXv9LN1OaVPAaLvV5swOlbccKmvHPnOA7L7Nis9g+/bRfnk3ivr3O11/kbfwmyUresOws3zYECrbKAlo1Agx0v45iTuc58avStdBcxCKeJrK45AV7oVOWsOL9bOQ5D6bjiBJ5IVpwujl4B6DlIhKKspTyYiuqyBNcSVKmkicP1ztdAJaxaE8TR8aITIWwbXhHS/yRwP4CHlDgHaNnfkzvBIy8LiNgqpBsDSP7XIygwghdnBGwRgYJKZDA5SGNLZOsEY42OLZMshVI1uUhjS2T8AjnJDS2THIVSI4CSdwuP/b8z1gmat4v4yTphUgSh5k49AUeKuL3FbxY2K47WZDGQVBdRou+K86SZVwaR2jL4z0HMpthE+LTqGSbPZWArahkaVSC51LJU1Ryj6TyPxm4gJRR28oJWPbcHHBVlO0qYBg7Zyow8bXABMjp0Gzp7ptjGw1JU3XHDeDrw4kcTXKag+JUjbQ6H4+UJuzNmpajSU7dtHk+mqpP8q6GZjOMJU0bq7k58DpYNVRXk5sIdHMTY01uDvsYUt/JJDQLq87cqWZ+HBaso8oHfsw076czeBr1cFe89glzKTsGVNfcaqeuMf5v7UBv5CHzwl4aqGb6NvA+KZ6lWa4OKh5Uzftt5B0Sr3GzjXjehUceVNcKN/EO2T/kTrry2ZqNkGHlU9cmrXzwJt/Rl6FY89pjWPHUzQdFsnbrskPO/MCzL1zbsqtKCVnQ5AcrYx4zrX7fexUWjHOWagTmLNflgehfXnU13YTVLvQkLX1CJwHNC+oTToNJzkpR89f7hnAvt0RHl65PfV+TB0EsGpB9Ktmq0ucUOWD13vViB040azhPkwaWfa40ULdXDIgTLqkY1Q57Awy/rqpNa4ECEeyaC3c/hMPqN42z5nTRnbqFuuQ2H3TfKKNeLthnm8zF4e5rg/eyvW820OMf</diagram></mxfile>
\ No newline at end of file
......@@ -21,11 +21,13 @@ If you have figured out the O(n) solution, try coding another solution of which
用滑动窗口来记录序列, 每当滑动窗口中的 sum 超过 s, 就去更新最小值,并根据先进先出的原则更新滑动窗口,直至 sum 刚好小于 s
![209.minimum-size-subarray-sum](../assets/problems/209.minimum-size-subarray-sum.png)
> 这道题目和 leetcode 3 号题目有点像,都可以用滑动窗口的思路来解决
## 关键点
- 滑动窗口简化操作
- 滑动窗口简化操作(滑窗口适合用于求解这种要求`连续`的题目)
## 代码
......
## 题目地址
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
Output: 3
Explanation: 12 = 4 + 4 + 4.
Example 2:
......@@ -23,19 +23,66 @@ Explanation: 13 = 4 + 9.
## 思路
直接递归处理即可,但是这种暴力的解法很容易超时。如果你把递归的过程化成一棵树的话(其实就是递归树),
可以看出中间有很多重复的计算。
可以看出中间有很多重复的计算。
如果能将重复的计算缓存下来,说不定能够解决时间复杂度太高的问题。
> 递归对内存的要求也很高, 如果数字非常大,也会面临爆栈的风险,将递归转化为循环可以解决。
如果使用DP,其实本质上和递归 + 缓存 差不多。
递归 + 缓存的方式代码如下:
```js
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);
};
```
如果使用 DP,其实本质上和递归 + 缓存 差不多。
DP 的代码见代码区。
## 关键点解析
- 如果用递归 + 缓存, 缓存的设计很重要
我的做法是key就是n,value是以n为起点,到达底端的深度。
下次取出缓存的时候用当前的level + 存的深度 就是我们想要的level.
我的做法是 key 就是 n,value 是以 n 为起点,到达底端的深度。
下次取出缓存的时候用当前的 level + 存的深度 就是我们想要的 level.
- 使用递归的核心点还是选和不选的问题
```js
for (let i = 1; i <= n; i++) {
for (let j = 1; j * j <= i; j++) {
// 选(dp[i]) 还是 不选(dp[i - j * j])
dp[i] = Math.min(dp[i], dp[i - j * j] + 1);
}
}
```
## 代码
......@@ -74,13 +121,12 @@ Explanation: 13 = 4 + 9.
const mapper = {};
function d(n, level) {
if (n === 0) return level;
if (n === 0) return level;
let i = 1;
const arr = [];
while(n - i * i >= 0) {
while (n - i * i >= 0) {
const hit = mapper[n - i * i];
if (hit) {
arr.push(hit + level);
......@@ -99,6 +145,19 @@ function d(n, level) {
* @return {number}
*/
var numSquares = function(n) {
return d(n, 0);
if (n <= 0) {
return 0;
}
const dp = Array(n + 1).fill(Number.MAX_VALUE);
dp[0] = 0;
for (let i = 1; i <= n; i++) {
for (let j = 1; j * j <= i; j++) {
// 选(dp[i]) 还是 不选(dp[i - j * j])
dp[i] = Math.min(dp[i], dp[i - j * j] + 1);
}
}
return dp[n];
};
```
/*
* @lc app=leetcode id=239 lang=javascript
*
* [239] Sliding Window Maximum
*
* https://leetcode.com/problems/sliding-window-maximum/description/
*
* algorithms
* Hard (37.22%)
* Total Accepted: 150.8K
* Total Submissions: 399.5K
* Testcase Example: '[1,3,-1,-3,5,3,6,7]\n3'
*
* Given an array nums, there is a sliding window of size k which is moving
* from the very left of the array to the very right. You can only see the k
* numbers in the window. Each time the sliding window moves right by one
* position. Return the max sliding window.
*
* Example:
*
*
* Input: nums = [1,3,-1,-3,5,3,6,7], and k = 3
* Output: [3,3,5,5,6,7]
* Explanation:
*
* Window position Max
* --------------- -----
* [1 3 -1] -3 5 3 6 7 3
* ⁠1 [3 -1 -3] 5 3 6 7 3
* ⁠1 3 [-1 -3 5] 3 6 7 5
* ⁠1 3 -1 [-3 5 3] 6 7 5
* ⁠1 3 -1 -3 [5 3 6] 7 6
* ⁠1 3 -1 -3 5 [3 6 7] 7
*
*
* Note:
* You may assume k is always valid, 1 ≤ k ≤ input array's size for non-empty
* array.
*
* Follow up:
* Could you solve it in linear time?
*/
/**
* @param {number[]} nums
* @param {number} k
* @return {number[]}
*/
var maxSlidingWindow = function(nums, k) {
// bad 时间复杂度O(n^2)
if (nums.length === 0 || k === 0) return [];
let slideWindow = [];
const ret = [];
for (let i = 0; i < nums.length - k + 1; i++) {
for (let j = 0; j < k; j++) {
slideWindow.push(nums[i + j]);
}
ret.push(Math.max(...slideWindow));
slideWindow = [];
}
return ret;
// 双端队列优化时间复杂度
};
/*
* @lc app=leetcode id=214 lang=javascript
*
* [214] Shortest Palindrome
*
* https://leetcode.com/problems/shortest-palindrome/description/
*
* algorithms
* Hard (27.15%)
* Total Accepted: 73.1K
* Total Submissions: 266.3K
* Testcase Example: '"aacecaaa"'
*
* Given a string s, you are allowed to convert it to a palindrome by adding
* characters in front of it. Find and return the shortest palindrome you can
* find by performing this transformation.
*
* Example 1:
*
*
* Input: "aacecaaa"
* Output: "aaacecaaa"
*
*
* Example 2:
*
*
* Input: "abcd"
* Output: "dcbabcd"
*/
/**
* @param {string} s
* @return {string}
*/
var shortestPalindrome = function(s) {
};
/*
* @lc app=leetcode id=5 lang=javascript
*
* [5] Longest Palindromic Substring
*
* https://leetcode.com/problems/longest-palindromic-substring/description/
*
* algorithms
* Medium (26.70%)
* Total Accepted: 525K
* Total Submissions: 1.9M
* Testcase Example: '"babad"'
*
* Given a string s, find the longest palindromic substring in s. You may
* assume that the maximum length of s is 1000.
*
* Example 1:
*
*
* Input: "babad"
* Output: "bab"
* Note: "aba" is also a valid answer.
*
*
* Example 2:
*
*
* Input: "cbbd"
* Output: "bb"
*
*
*/
/**
* @param {string} s
* @return {string}
*/
var longestPalindrome = function(s) {
};
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册