Skip to content

Commit 948ade1

Browse files
author
lucifer
committed
fix: 不用 github cdn
1 parent 17abf18 commit 948ade1

5 files changed

+47
-248
lines changed

SUMMARY.md

Lines changed: 19 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -26,22 +26,22 @@
2626
* [单调栈](thinkings/monotone-stack.md)
2727

2828

29-
* [第二章 - 91 天学算法](./91/README.md)
30-
* [第一期讲义-二分法](./91/binary-search.md)
31-
* [第一期讲义-双指针](./91/two-pointers.md)
32-
* [第二期](./91/season2.md)
29+
* [第二章 - 91 天学算法](91/README.md)
30+
* [第一期讲义-二分法](91/binary-search.md)
31+
* [第一期讲义-双指针](91/two-pointers.md)
32+
* [第二期](91/season2.md)
3333

3434

35-
* [第三章 - 精选题解](./selected/README.md)
36-
* [字典序列删除](./selected/a-deleted.md)
37-
* [西法的刷题秘籍】一次搞定前缀和](./selected/atMostK.md)
38-
* [字节跳动的算法面试题是什么难度?](./selected/byte-dance-algo-ex.md)
39-
* [字节跳动的算法面试题是什么难度?(第二弹)](./selected/byte-dance-algo-ex-2017.md)
40-
* [《我是你的妈妈呀》 * 第一期](./selected/mother-01.md)
41-
* [一文带你看懂二叉树的序列化](./selected/serialize.md)
42-
* [穿上衣服我就不认识你了?来聊聊最长上升子序列](./selected/LIS.md)
43-
* [你的衣服我扒了 * 《最长公共子序列》](./selected/LCS.md)
44-
* [一文看懂《最大子序列和问题》](./selected/LSS.md)
35+
* [第三章 - 精选题解](selected/README.md)
36+
* [字典序列删除](selected/a-deleted.md)
37+
* [西法的刷题秘籍】一次搞定前缀和](selected/atMostK.md)
38+
* [字节跳动的算法面试题是什么难度?](selected/byte-dance-algo-ex.md)
39+
* [字节跳动的算法面试题是什么难度?(第二弹)](selected/byte-dance-algo-ex-2017.md)
40+
* [《我是你的妈妈呀》 * 第一期](selected/mother-01.md)
41+
* [一文带你看懂二叉树的序列化](selected/serialize.md)
42+
* [穿上衣服我就不认识你了?来聊聊最长上升子序列](selected/LIS.md)
43+
* [你的衣服我扒了 * 《最长公共子序列》](selected/LCS.md)
44+
* [一文看懂《最大子序列和问题》](selected/LSS.md)
4545

4646

4747
* [第四章 - 高频考题(简单)](collections/easy.md)
@@ -110,7 +110,7 @@
110110
* [0055. 跳跃游戏](problems/55.jump-game.md)
111111
* [0056. 合并区间](problems/56.merge-intervals.md)
112112
* [0060. 第k个排列](problems/60.permutation-sequence.md)
113-
* [0061. 旋转链表](../problems/61.Rotate-List.md) 91
113+
* [0061. 旋转链表](problems/61.Rotate-List.md) 91
114114
* [0062. 不同路径](problems/62.unique-paths.md)
115115
* [0073. 矩阵置零](problems/73.set-matrix-zeroes.md)
116116
* [0075. 颜色分类](problems/75.sort-colors.md)
@@ -133,7 +133,7 @@
133133
* [0131. 分割回文串](problems/131.palindrome-partitioning.md)
134134
* [0139. 单词拆分](problems/139.word-break.md)
135135
* [0144. 二叉树的前序遍历](problems/144.binary-tree-preorder-traversal.md)
136-
* [0147. 对链表进行插入排序](../problems/147.insertion-sort-list.md)
136+
* [0147. 对链表进行插入排序](problems/147.insertion-sort-list.md)
137137
* [0150. 逆波兰表达式求值](problems/150.evaluate-reverse-polish-notation.md)
138138
* [0152. 乘积最大子数组](problems/152.maximum-product-subarray.md)
139139
* [0199. 二叉树的右视图](problems/199.binary-tree-right-side-view.md)
@@ -211,6 +211,7 @@
211211
* [1631. 最小体力消耗路径](problems/1631.path-with-minimum-effort.md)
212212
* [1658. 将 x 减到 0 的最小操作数](problems/1658.minimum-operations-to-reduce-x-to-zero.md)
213213
* [面试题 17.23. 最大黑方阵](problems/max-black-square-lcci.md)
214+
* [面试题 17.09. 第 k 个数](problems/get-kth-magic-number-lcci.md)
214215

215216

216217
* [第六章 - 高频考题(困难)](collections/hard.md)
@@ -239,7 +240,7 @@
239240
* [0472. 连接词](problems/472.concatenated-words.md)
240241
* [0488. 祖玛游戏](problems/488.zuma-game.md)
241242
* [0493. 翻转对](problems/493.reverse-pairs.md)
242-
* [0768. 最多能完成排序的块 II](../problems/768.max-chunks-to-make-sorted-ii.md) 91
243+
* [0768. 最多能完成排序的块 II](problems/768.max-chunks-to-make-sorted-ii.md) 91
243244
* [0887. 鸡蛋掉落](problems/887.super-egg-drop.md)
244245
* [0895. 最大频率栈](problems/895.maximum-frequency-stack.md)
245246
* [1032. 字符流](problems/1032.stream-of-characters.md)
@@ -248,4 +249,4 @@
248249
* [1449. 数位成本和为目标值的最大数字](problems/1449.form-largest-integer-with-digits-that-add-up-to-target.md)
249250

250251

251-
* [后序](./epilogue.md)
252+
* [后序](epilogue.md)

collections/medium.md

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,8 @@
66

77
以下是我列举的经典题目(带 91 字样的表示出自 **91 天学算法**活动):
88

9-
- [面试题 17.09. 第 k 个数](../../problems/get-kth-magic-number-lcci.md)
9+
- [面试题 17.09. 第 k 个数](../problems/get-kth-magic-number-lcci.md)
10+
- [面试题 17.23. 最大黑方阵](../problems/max-black-square-lcci.md)🆕
1011

1112
- [0002. 两数相加](../problems/2.add-two-numbers.md)
1213
- [0003. 无重复字符的最长子串](../problems/3.longest-substring-without-repeating-characters.md)
@@ -130,4 +131,4 @@
130131
- [1574. 删除最短的子数组使剩余数组有序](../problems/1574.shortest-subarray-to-be-removed-to-make-array-sorted.md) 🆕
131132
- [1631. 最小体力消耗路径](../problems/1631.path-with-minimum-effort.md) 🆕
132133
- [1658. 将 x 减到 0 的最小操作数](../problems/1658.minimum-operations-to-reduce-x-to-zero.md) 🆕
133-
- [面试题 17.23. 最大黑方阵](../problems/max-black-square-lcci.md)🆕
134+

problems/1381.design-a-stack-with-increment-operation.md

Lines changed: 13 additions & 131 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
# 题目地址(1381. 设计一个支持增量操作的栈)
22

3-
https://leetcode-cn.com/problems/design-a-stack-with-increment-operation/
3+
https://leetcode-cn.com/problems/plus-one
44

55
## 题目描述
66

@@ -52,11 +52,11 @@ customStack.pop(); // 返回 -1 --> 栈为空,返回 -1
5252
-
5353
- 前缀和
5454

55-
## increment 时间复杂度为 $$O(k)$$ 的方法
55+
## increment 时间复杂度为 $O(k)$ 的方法
5656

5757
### 思路
5858

59-
首先我们来看一种非常符合直觉的方法,然而这种方法并不好,increment 操作需要的时间复杂度为 $$O(k)$$
59+
首先我们来看一种非常符合直觉的方法,然而这种方法并不好,increment 操作需要的时间复杂度为 $O(k)$。
6060

6161
`push``pop` 就是普通的栈操作。 唯一要注意的是边界条件,这个已经在题目中指明了,具体来说就是:
6262

@@ -95,8 +95,8 @@ class CustomStack:
9595

9696
**复杂度分析**
9797

98-
- 时间复杂度:push 和 pop 操作的时间复杂度为 $$O(1)$$(讲义有提到),而 increment 操作的时间复杂度为 $$O(min(k, cnt))$$
99-
- 空间复杂度:$$O(1)$$
98+
- 时间复杂度:push 和 pop 操作的时间复杂度为 $O(1)$(讲义有提到),而 increment 操作的时间复杂度为 $O(min(k, cnt))$
99+
- 空间复杂度:$O(1)$
100100

101101
## 前缀和
102102

@@ -112,7 +112,7 @@ class CustomStack:
112112
- push 操作不变,和上面一样
113113
- increment 的时候,我们将用到 incremental 信息。那么这个信息是什么,从哪来呢?我这里画了一个图
114114

115-
![image](https://user-images.githubusercontent.com/12479470/83656933-c096d300-a5f2-11ea-8f50-64ced5aa62f2.png)
115+
![](https://tva1.sinaimg.cn/large/0081Kckwly1glwx11x0l0j30u014itck.jpg)
116116

117117
如图黄色部分是我们需要执行增加操作,我这里画了一个挡板分割,实际上这个挡板不存在。那么如何记录黄色部分的信息呢?我举个例子来说
118118

@@ -121,15 +121,14 @@ class CustomStack:
121121
- 调用了 increment(3, 2),就把 increment[3] 增加 2。
122122
- 继续调用 increment(2, 5),就把 increment[2] 增加 5。
123123

124-
![image](https://user-images.githubusercontent.com/12479470/83640207-6855d600-a5de-11ea-809e-bba303927707.png)
124+
![](https://tva1.sinaimg.cn/large/0081Kckwly1glwx1fk7vcj30nm0c8wfb.jpg)
125125

126126
而当我们 pop 的时候:
127127

128128
- 只需要将栈顶元素**加上 increment[cnt - 1]** 即可, 其中 cnt 为栈当前的大小。
129129
- 另外,我们需要将 increment[cnt - 1] 更新到 increment[cnt - 2],并将 increment[cnt - 1] 重置为 0。
130130

131-
![image](https://user-images.githubusercontent.com/12479470/83640238-7146a780-a5de-11ea-8b81-81439353068f.png)
132-
131+
![](https://tva1.sinaimg.cn/large/0081Kckwly1glwx1ryzxpj31jq0hijte.jpg)
133132
### 代码
134133

135134
```py
@@ -164,25 +163,21 @@ class CustomStack:
164163

165164
**复杂度分析**
166165

167-
- 时间复杂度:全部都是 $$O(1)$$
168-
- 空间复杂度:我们维护了一个大小为 maxSize 的数组,因此平均到每次的空间复杂度为 $$O(maxSize / N)$$,其中 N 为操作数。
166+
- 时间复杂度:全部都是 $O(1)$
167+
- 空间复杂度:我们维护了一个大小为 maxSize 的数组,因此平均到每次的空间复杂度为 $O(maxSize / N)$,其中 N 为操作数。
169168

170169
## 优化的前缀和
171170

172171
### 思路
173172

174-
上面的思路无论如何,我们都需要维护一个大小为 $$O(maxSize)$$ 的数组 incremental 。而由于栈只能在栈顶进行操作,因此这实际上可以稍微优化一点,即维护一个大小为当前栈长度的 incrementals,而不是 $$O(maxSize)$$
173+
上面的思路无论如何,我们都需要维护一个大小为 $O(maxSize)$ 的数组 incremental 。而由于栈只能在栈顶进行操作,因此这实际上可以稍微优化一点,即维护一个大小为当前栈长度的 incrementals,而不是 $O(maxSize)$ 。
175174

176175
每次栈 push 的时候,incrementals 也 push 一个 0。每次栈 pop 的时候, incrementals 也 pop,这样就可以了。
177176

178177
> 这里的 incrementals 并不是一个栈,而是一个普通数组,因此可以随机访问。
179178
180179
### 代码
181180

182-
代码支持: Python3, Go, PHP
183-
184-
Python3 Code:
185-
186181
```py
187182
class CustomStack:
188183

@@ -212,123 +207,10 @@ class CustomStack:
212207
self.incrementals[min(self.cnt, k) - 1] += val
213208
```
214209

215-
Go Code:
216-
217-
```go
218-
type CustomStack struct {
219-
Stack []int
220-
PreSum []int // 前缀和
221-
Cnt int
222-
Size int
223-
Top int
224-
}
225-
226-
func Constructor(maxSize int) CustomStack {
227-
// 因为 go 语言 slice 底层实现机制, 如果不设置 cap, 会导致频繁扩容, 可能增大时间/内存消耗
228-
// 提交测试后的最佳组合: Stack 使用默认值, PreSum 预设为 MaxSize
229-
return CustomStack{Size: maxSize, PreSum: make([]int, maxSize, maxSize)}
230-
}
231-
232-
func (this *CustomStack) Push(x int) {
233-
if this.Cnt < this.Size {
234-
this.Stack = append(this.Stack, x)
235-
this.Cnt++
236-
}
237-
}
238-
239-
func (this *CustomStack) Pop() int {
240-
if this.Cnt == 0 {
241-
return -1
242-
}
243-
n := len(this.Stack)
244-
var a int
245-
a, this.Stack = this.Stack[n-1], this.Stack[:n-1]
246-
this.Top = a + this.PreSum[n-1]
247-
if this.Cnt>=2 { // 重置
248-
this.PreSum[n-2] += this.PreSum[n-1]
249-
}
250-
this.PreSum[n-1] = 0
251-
this.Cnt--
252-
return this.Top
253-
}
254-
255-
func (this *CustomStack) Increment(k int, val int) {
256-
if this.Cnt == 0 {
257-
return
258-
}
259-
n := min(k, this.Cnt)
260-
this.PreSum[n-1] += val
261-
}
262-
263-
func min(a, b int) int {
264-
if a < b {
265-
return a
266-
}
267-
return b
268-
}
269-
```
270-
271-
PHP Code:
272-
273-
```php
274-
class customStack
275-
{
276-
public $stack = [];
277-
public $preSum = [];
278-
public $size;
279-
public $cnt = 0;
280-
public $top = -1; // debug
281-
282-
/**
283-
* @param Integer $maxSize
284-
*/
285-
function __construct($maxSize)
286-
{
287-
$this->size = $maxSize;
288-
}
289-
290-
/**
291-
* @param Integer $x
292-
* @return NULL
293-
*/
294-
function push($x)
295-
{
296-
if ($this->cnt < $this->size) {
297-
array_push($this->stack, $x);
298-
array_push($this->preSum, 0);
299-
$this->cnt++;
300-
}
301-
}
302-
303-
/**
304-
* @return Integer
305-
*/
306-
function pop()
307-
{
308-
if (!$this->cnt) return -1;
309-
if ($this->cnt >= 2) $this->preSum[$this->cnt - 2] += $this->preSum[$this->cnt - 1];
310-
$this->cnt--;
311-
$this->top = array_pop($this->stack) + array_pop($this->preSum);
312-
return $this->top;
313-
}
314-
315-
/**
316-
* @param Integer $k
317-
* @param Integer $val
318-
* @return NULL
319-
*/
320-
function increment($k, $val)
321-
{
322-
$k = min($k, $this->cnt);
323-
if ($k) $this->preSum[$k - 1] += $val;
324-
}
325-
}
326-
```
327-
328210
**复杂度分析**
329211

330-
- 时间复杂度:全部都是 $$O(1)$$
331-
- 空间复杂度:我们维护了一个大小为 cnt 的数组,因此平均到每次的空间复杂度为 $$O(cnt / N)$$,其中 N 为操作数,cnt 为操作过程中的栈的最大长度(小于等于 maxSize)。
212+
- 时间复杂度:全部都是 $O(1)$
213+
- 空间复杂度:我们维护了一个大小为 cnt 的数组,因此平均到每次的空间复杂度为 $O(cnt / N)$,其中 N 为操作数,cnt 为操作过程中的栈的最大长度(小于等于 maxSize)。
332214

333215
可以看出优化的解法在 maxSize 非常大的时候是很有意义的。
334216

problems/1631.path-with-minimum-effort.md

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -61,8 +61,8 @@ columns == heights[i].length
6161
## 前置知识
6262

6363
- 二维矩阵
64-
- [深度优先遍历](../thinkings/DFS.md)
65-
- [二分查找](../91/binary-search.md)
64+
- [深度优先遍历](https://github.com/azl397985856/leetcode/blob/master/thinkings/DFS.md)
65+
- [二分查找](https://github.com/azl397985856/leetcode/blob/master/91/binary-search.md)
6666

6767
## 公司
6868

@@ -74,7 +74,7 @@ columns == heights[i].length
7474

7575
由于题目的解空间是 [0, 10**6 - 1]
7676

77-
> 对解空间这个概念不熟悉的,可以看我之前的一篇题解[686. 重复叠加字符串匹配](./686.repeated-string-match.md)
77+
> 对解空间这个概念不熟悉的,可以看我之前的一篇题解[686. 重复叠加字符串匹配](https://github.com/azl397985856/leetcode/blob/master/problems/686.repeated-string-match.md)
7878
7979
本质上,我们需要进行发问:
8080

@@ -89,7 +89,7 @@ columns == heights[i].length
8989

9090
实际上,上面的不断发问的过程不就是一个连续的递增序列么? 我们的目标不就是在一个连续递增序列找指定值么?于是二分法就不难想到。
9191

92-
而且这道题本质就是二分查找中的**查找最右侧满足条件的值**,关于这个问题,我已经在 [【91 天学算法】二分查找](../91/binary-search.md) 中进行了详细描述,并给出了代码模板,直接套就可以了。
92+
而且这道题本质就是二分查找中的**查找最右侧满足条件的值**,关于这个问题,我已经在 [【91 天学算法】二分查找](https://github.com/azl397985856/leetcode/blob/master/91/binary-search.md) 中进行了详细描述,并给出了代码模板,直接套就可以了。
9393

9494
值得注意的是,我们只需要找到一个满足条件的路径即可,因此可以利用短路剪枝。
9595

@@ -142,4 +142,4 @@ m 为 矩阵的高度, n 为矩阵的长度。
142142

143143
## 相关问题
144144

145-
- [875. 爱吃香蕉的珂珂](./875.koko-eating-bananas.md)
145+
- [875. 爱吃香蕉的珂珂](https://github.com/azl397985856/leetcode/blob/master/problems/875.koko-eating-bananas.md)

0 commit comments

Comments
 (0)