Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit 80f7539

Browse files
authored
Merge pull request azl397985856#2 from azl397985856/master
update fork
2 parents 9319ea2 + cdec1bd commit 80f7539

File tree

5 files changed

+145
-94
lines changed

5 files changed

+145
-94
lines changed

README.md

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -267,7 +267,7 @@ leetcode 题解,记录自己的 leetcode 解题之路。
267267
- [0295.find-median-from-data-stream](./problems/295.find-median-from-data-stream.md) 🆕
268268
- [0301.remove-invalid-parentheses](./problems/301.remove-invalid-parentheses.md)
269269
- [0335.self-crossPing](./problems/335.self-crossing.md) 🆕
270-
- [0460.lfu-cache](./problems/460.lfu-cache.md)
270+
- [0460.lfu-cache](./problems/460.lfu-cache.md)
271271
- [0472.concatenated-words](./problems/472.concatenated-words.md) 🆕
272272
- [0493.reverse-pairs](./problems/493.reverse-pairs.md) 🆕
273273
- [0895.maximum-frequency-stack](./problems/895.maximum-frequency-stack.md) 🆕
@@ -290,6 +290,8 @@ leetcode 题解,记录自己的 leetcode 解题之路。
290290
- [滑动窗口(思路 + 模板)](./thinkings/slide-window.md) 🆕
291291
- [位运算](./thinkings/bit.md) 🆕
292292
- [设计题](./thinkings/design.md) 🆕
293+
- [小岛问题](./thinkings/island.md) 🆕
294+
- [最大公约数](./thinkings/GCD.md) 🆕
293295

294296
### anki 卡片
295297

problems/1011.capacity-to-ship-packages-within-d-days.md

Lines changed: 58 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -66,17 +66,16 @@ https://leetcode-cn.com/problems/capacity-to-ship-packages-within-d-days
6666
```python
6767
def canShip(opacity):
6868
# 指定船的容量是否可以在D天运完
69-
lo = 0
70-
hi = total
71-
while lo < hi:
72-
mid = (lo + hi) // 2
73-
if canShip(mid):
74-
hi = mid
75-
else:
76-
lo = mid + 1
77-
78-
return lo
79-
69+
lo = 0
70+
hi = total
71+
while lo < hi:
72+
mid = (lo + hi) // 2
73+
if canShip(mid):
74+
hi = mid
75+
else:
76+
lo = mid + 1
77+
78+
return lo
8079
```
8180

8281
## 关键点解析
@@ -85,6 +84,10 @@ return lo
8584

8685
## 代码
8786

87+
* 语言支持:`JS``Python`
88+
89+
`python`:
90+
8891
```python
8992
class Solution:
9093
def shipWithinDays(self, weights: List[int], D: int) -> int:
@@ -115,6 +118,50 @@ class Solution:
115118
return lo
116119
```
117120

121+
`js`:
122+
123+
```js
124+
/**
125+
* @param {number[]} weights
126+
* @param {number} D
127+
* @return {number}
128+
*/
129+
var shipWithinDays = function(weights, D) {
130+
let high = weights.reduce((acc, cur) => acc + cur)
131+
let low = 0
132+
133+
while(low < high) {
134+
let mid = Math.floor((high + low) / 2)
135+
if (canShip(mid)) {
136+
high = mid
137+
} else {
138+
low = mid + 1
139+
}
140+
}
141+
142+
return low
143+
144+
function canShip(opacity) {
145+
let remain = opacity
146+
let count = 1
147+
for (let weight of weights) {
148+
if (weight > opacity) {
149+
return false
150+
}
151+
remain -= weight
152+
if (remain < 0) {
153+
count++
154+
remain = opacity - weight
155+
}
156+
if (count > D) {
157+
return false
158+
}
159+
}
160+
return count <= D
161+
}
162+
};
163+
```
164+
118165
## 扩展
119166

120167
## 参考

problems/887.super-egg-drop.md

Lines changed: 1 addition & 82 deletions
Original file line numberDiff line numberDiff line change
@@ -108,88 +108,8 @@ Note:
108108

109109
## 代码
110110

111-
```js
112111

113-
/*
114-
* @lc app=leetcode id=887 lang=javascript
115-
*
116-
* [887] Super Egg Drop
117-
*
118-
* https://leetcode.com/problems/super-egg-drop/description/
119-
*
120-
* algorithms
121-
* Hard (24.64%)
122-
* Total Accepted: 6.2K
123-
* Total Submissions: 24.9K
124-
* Testcase Example: '1\n2'
125-
*
126-
* You are given K eggs, and you have access to a building with N floors from 1
127-
* to N.
128-
*
129-
* Each egg is identical in function, and if an egg breaks, you cannot drop it
130-
* again.
131-
*
132-
* You know that there exists a floor F with 0 <= F <= N such that any egg
133-
* dropped at a floor higher than F will break, and any egg dropped at or below
134-
* floor F will not break.
135-
*
136-
* Each move, you may take an egg (if you have an unbroken one) and drop it
137-
* from any floor X (with 1 <= X <= N).
138-
*
139-
* Your goal is to know with certainty what the value of F is.
140-
*
141-
* What is the minimum number of moves that you need to know with certainty
142-
* what F is, regardless of the initial value of F?
143-
*
144-
*
145-
*
146-
*
147-
*
148-
*
149-
*
150-
* Example 1:
151-
*
152-
*
153-
* Input: K = 1, N = 2
154-
* Output: 2
155-
* Explanation:
156-
* Drop the egg from floor 1. If it breaks, we know with certainty that F = 0.
157-
* Otherwise, drop the egg from floor 2. If it breaks, we know with certainty
158-
* that F = 1.
159-
* If it didn't break, then we know with certainty F = 2.
160-
* Hence, we needed 2 moves in the worst case to know what F is with
161-
* certainty.
162-
*
163-
*
164-
*
165-
* Example 2:
166-
*
167-
*
168-
* Input: K = 2, N = 6
169-
* Output: 3
170-
*
171-
*
172-
*
173-
* Example 3:
174-
*
175-
*
176-
* Input: K = 3, N = 14
177-
* Output: 4
178-
*
179-
*
180-
*
181-
*
182-
* Note:
183-
*
184-
*
185-
* 1 <= K <= 100
186-
* 1 <= N <= 10000
187-
*
188-
*
189-
*
190-
*
191-
*
192-
*/
112+
```js
193113
/**
194114
* @param {number} K
195115
* @param {number} N
@@ -205,7 +125,6 @@ var superEggDrop = function(K, N) {
205125
for (let k = 1; k <= K; ++k)
206126
dp[m][k] = dp[m - 1][k - 1] + 1 + dp[m - 1][k];
207127
}
208-
console.log(dp);
209128
return m;
210129
};
211130
```

thinkings/GCD.md

Lines changed: 74 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,74 @@
1+
# 最大公约数
2+
3+
关于最大公约数有专门的研究。 而在 LeetCode 中虽然没有直接让你求解最大公约数的题目。但是却有一些间接需要你求解最大公约数的题目。
4+
5+
比如:
6+
7+
- [914. 卡牌分组](https://leetcode-cn.com/problems/x-of-a-kind-in-a-deck-of-cards/solution/python3-zui-da-gong-yue-shu-914-qia-pai-fen-zu-by-/)
8+
- [365. 水壶问题](https://leetcode-cn.com/problems/water-and-jug-problem/solution/bfszui-da-gong-yue-shu-by-fe-lucifer/)
9+
- [1071. 字符串的最大公因子](https://leetcode-cn.com/problems/greatest-common-divisor-of-strings/solution/1071-zi-fu-chuan-de-zui-da-gong-yin-zi-zui-da-gong/)
10+
11+
因此如何求解最大公约数就显得重要了。
12+
13+
## 定义法
14+
15+
```python
16+
def GCD(a: int, b: int) -> int:
17+
smaller = min(a, b)
18+
while smaller:
19+
if a % smaller == 0 and b % smaller == 0:
20+
return smaller
21+
smaller -= 1
22+
```
23+
24+
**复杂度分析**
25+
26+
- 时间复杂度:最好的情况是执行一次循环体,最坏的情况是循环到 smaller 为 1,因此总的时间复杂度为 $O(N)$,其中 N 为 a 和 b 中较小的数。
27+
- 空间复杂度:$O(1)$。
28+
29+
## 辗转相除法
30+
31+
如果我们需要计算 a 和 b 的最大公约数,运用辗转相除法的话。首先,我们先计算出 a 除以 b 的余数 c,把问题转化成求出 b 和 c 的最大公约数;然后计算出 b 除以 c 的余数 d,把问题转化成求出 c 和 d 的最大公约数;再然后计算出 c 除以 d 的余数 e,把问题转化成求出 d 和 e 的最大公约数。..... 以此类推,逐渐把两个较大整数之间的运算转化为两个较小整数之间的运算,直到两个数可以整除为止。
32+
33+
```python
34+
def GCD(a: int, b: int) -> int:
35+
return a if b == 0 else GCD(b, a % b)
36+
```
37+
38+
**复杂度分析**
39+
40+
- 时间复杂度:$O(log(max(a, b)))$
41+
- 空间复杂度:空间复杂度取决于递归的深度,因此空间复杂度为 $O(log(max(a, b)))$
42+
43+
下面我们对上面的过程进行一个表形象地讲解,实际上这也是教材里面的讲解方式,我只是照搬过来,增加一下自己的理解罢了。我们来通过一个例子来讲解:
44+
45+
假如我们有一块 1680 米 \* 640 米 的土地,我们希望讲起分成若干正方形的土地,且我们想让正方形土地的边长尽可能大,我们应该如何设计算法呢?
46+
47+
实际上这正是一个最大公约数的应用场景,我们的目标就是求解 1680 和 640 的最大公约数。
48+
49+
![](https://tva1.sinaimg.cn/large/00831rSTly1gdflglk6v2j30f104zgo0.jpg)
50+
51+
将 1680 米 \* 640 米 的土地分割,相当于对将 400 米 \* 640 米 的土地进行分割。 为什么呢? 假如 400 米 \* 640 米分割的正方形边长为 x,那么有 640 % x == 0,那么肯定也满足剩下的两块 640 米 \* 640 米的。
52+
53+
![](https://tva1.sinaimg.cn/large/00831rSTly1gdfliql4tvj30g805aq5k.jpg)
54+
55+
我们不断进行上面的分割:
56+
57+
![](https://tva1.sinaimg.cn/large/00831rSTly1gdflles5bsj307x08vgmv.jpg)
58+
59+
直到边长为 80,没有必要进行下去了。
60+
61+
![](https://tva1.sinaimg.cn/large/00831rSTly1gdfllz6hx4j30aa04uwer.jpg)
62+
63+
辗转相除法如果 a 和 b 都很大的时候,a % b 性能会较低。在中国,《九章算术》中提到了一种类似辗转相减法的 [更相减损术](https://zh.wikisource.org/wiki/%E4%B9%9D%E7%AB%A0%E7%AE%97%E8%A1%93#-.7BA.7Czh-hans:.E5.8D.B7.3Bzh-hant:.E5.8D.B7.7D-.E7.AC.AC.E4.B8.80.E3.80.80.E6.96.B9.E7.94.B0.E4.BB.A5.E5.BE.A1.E7.94.B0.E7.96.87.E7.95.8C.E5.9F.9F "更相减损术")。它的原理是:`两个正整数 a 和 b(a>b),它们的最大公约数等于 a-b 的差值 c 和较小数 b 的最大公约数。`
64+
65+
```python
66+
def GCD(a: int, b: int) -> int:
67+
if a == b:
68+
return a
69+
if a < b:
70+
return GCD(b - a, a)
71+
return GCD(a - b, b)
72+
```
73+
74+
上面的代码会报栈溢出。原因在于如果 a 和 b 相差比较大的话,递归次数会明显增加,要比辗转相除法递归深度增加很多,最坏时间复杂度为 O(max(a, b)))。这个时候我们可以将`辗转相除法``更相减损术`做一个结合,从而在各种情况都可以获得较好的性能。

thinkings/island.md

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
# 小岛
2+
3+
LeetCode 上有很多小岛题,虽然官方没有这个标签, 但是在我这里都差不多。不管是思路还是套路都比较类似,大家可以结合起来练习。
4+
5+
- [200. 岛屿数量](https://github.com/azl397985856/leetcode/blob/master/problems/200.number-of-islands.md)
6+
- [695. 岛屿的最大面积](https://leetcode-cn.com/problems/max-area-of-island/solution/695-dao-yu-de-zui-da-mian-ji-dfspython3-by-fe-luci/)
7+
- [1162. 地图分析](https://leetcode-cn.com/problems/as-far-from-land-as-possible/solution/python-tu-jie-chao-jian-dan-de-bfs1162-di-tu-fen-x/)
8+
9+
上面三道题都可以使用常规的 DFS 来做。 并且递归的方向都是上下左右四个方向。更有意思的是,都可以采用原地修改的方式,来减少开辟 visited 的空间。

0 commit comments

Comments
 (0)