Skip to content

Commit ba7bc76

Browse files
committed
更新题解列表
1 parent 3b8a101 commit ba7bc76

File tree

5 files changed

+61
-4
lines changed

5 files changed

+61
-4
lines changed

Contents/00.Introduction/04.Solutions-List.md

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
# LeetCode 题解(已完成 608 道)
1+
# LeetCode 题解(已完成 609 道)
22

33
| 题号 | 标题 | 题解 | 标签 | 难度 |
44
| :------ | :------ | :------ | :------ | :------ |
@@ -370,6 +370,7 @@
370370
| 0881 | [救生艇](https://leetcode-cn.com/problems/boats-to-save-people/) | [Python](https://github.com/itcharge/LeetCode-Py/blob/main/Solutions/0881.%20%E6%95%91%E7%94%9F%E8%89%87.md) | 贪心、数组、双指针、排序 | 中等 |
371371
| 0886 | [可能的二分法](https://leetcode-cn.com/problems/possible-bipartition/) | [Python](https://github.com/itcharge/LeetCode-Py/blob/main/Solutions/0886.%20%E5%8F%AF%E8%83%BD%E7%9A%84%E4%BA%8C%E5%88%86%E6%B3%95.md) | 深度优先搜索、广度优先搜索、并查集、图 | 中等 |
372372
| 0897 | [递增顺序搜索树](https://leetcode-cn.com/problems/increasing-order-search-tree/) | [Python](https://github.com/itcharge/LeetCode-Py/blob/main/Solutions/0897.%20%E9%80%92%E5%A2%9E%E9%A1%BA%E5%BA%8F%E6%90%9C%E7%B4%A2%E6%A0%91.md) | 栈、树、深度优先搜索、二叉搜索树、二叉树 | 简单 |
373+
| 0904 | [水果成篮](https://leetcode-cn.com/problems/fruit-into-baskets/) | [Python](https://github.com/itcharge/LeetCode-Py/blob/main/Solutions/0904.%20%E6%B0%B4%E6%9E%9C%E6%88%90%E7%AF%AE.md) | 数组、哈希表、滑动窗口 | 中等 |
373374
| 0908 | [最小差值 I](https://leetcode-cn.com/problems/smallest-range-i/) | [Python](https://github.com/itcharge/LeetCode-Py/blob/main/Solutions/0908.%20%E6%9C%80%E5%B0%8F%E5%B7%AE%E5%80%BC%20I.md) | 数组、数学 | 简单 |
374375
| 0912 | [排序数组](https://leetcode-cn.com/problems/sort-an-array/) | [Python](https://github.com/itcharge/LeetCode-Py/blob/main/Solutions/0912.%20%E6%8E%92%E5%BA%8F%E6%95%B0%E7%BB%84.md) | 数组、分治、桶排序、计数排序、基数排序、排序、堆(优先队列) | 中等 |
375376
| 0918 | [环形子数组的最大和](https://leetcode-cn.com/problems/maximum-sum-circular-subarray/) | [Python](https://github.com/itcharge/LeetCode-Py/blob/main/Solutions/0918.%20%E7%8E%AF%E5%BD%A2%E5%AD%90%E6%95%B0%E7%BB%84%E7%9A%84%E6%9C%80%E5%A4%A7%E5%92%8C.md) | 数组、动态规划 | 中等 |

Contents/00.Introduction/05.Categories-List.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -247,7 +247,7 @@
247247
| 0795 | [区间子数组个数](https://leetcode-cn.com/problems/number-of-subarrays-with-bounded-maximum/) | [Python](https://github.com/itcharge/LeetCode-Py/blob/main/Solutions/0795.%20%E5%8C%BA%E9%97%B4%E5%AD%90%E6%95%B0%E7%BB%84%E4%B8%AA%E6%95%B0.md) | 数组、双指针 | 中等 |
248248
| 0992 | K 个不同整数的子数组 | | | |
249249
| 0713 | [乘积小于K的子数组](https://leetcode-cn.com/problems/subarray-product-less-than-k/) | [Python](https://github.com/itcharge/LeetCode-Py/blob/main/Solutions/0713.%20%E4%B9%98%E7%A7%AF%E5%B0%8F%E4%BA%8EK%E7%9A%84%E5%AD%90%E6%95%B0%E7%BB%84.md) | 数组、滑动窗口 | 中等 |
250-
| 0904 | 水果成篮 | | | |
250+
| 0904 | [水果成篮](https://leetcode-cn.com/problems/fruit-into-baskets/) | [Python](https://github.com/itcharge/LeetCode-Py/blob/main/Solutions/0904.%20%E6%B0%B4%E6%9E%9C%E6%88%90%E7%AF%AE.md) | 数组、哈希表、滑动窗口 | 中等 |
251251
| 1358 | 包含所有三种字符的子字符串数目 | | | |
252252
| 0467 | 环绕字符串中唯一的子字符串 | | | |
253253
| 0220 | [存在重复元素 III](https://leetcode-cn.com/problems/contains-duplicate-iii/) | [Python](https://github.com/itcharge/LeetCode-Py/blob/main/Solutions/0220.%20%E5%AD%98%E5%9C%A8%E9%87%8D%E5%A4%8D%E5%85%83%E7%B4%A0%20III.md) | 排序、有序集合、哈希表 | 中等 |

Contents/01.Array/05.Array-Sliding-Window/10.Array-Sliding-Window-List.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -40,7 +40,7 @@
4040
| 0795 | [区间子数组个数](https://leetcode-cn.com/problems/number-of-subarrays-with-bounded-maximum/) | [Python](https://github.com/itcharge/LeetCode-Py/blob/main/Solutions/0795.%20%E5%8C%BA%E9%97%B4%E5%AD%90%E6%95%B0%E7%BB%84%E4%B8%AA%E6%95%B0.md) | 数组、双指针 | 中等 |
4141
| 0992 | K 个不同整数的子数组 | | | |
4242
| 0713 | [乘积小于K的子数组](https://leetcode-cn.com/problems/subarray-product-less-than-k/) | [Python](https://github.com/itcharge/LeetCode-Py/blob/main/Solutions/0713.%20%E4%B9%98%E7%A7%AF%E5%B0%8F%E4%BA%8EK%E7%9A%84%E5%AD%90%E6%95%B0%E7%BB%84.md) | 数组、滑动窗口 | 中等 |
43-
| 0904 | 水果成篮 | | | |
43+
| 0904 | [水果成篮](https://leetcode-cn.com/problems/fruit-into-baskets/) | [Python](https://github.com/itcharge/LeetCode-Py/blob/main/Solutions/0904.%20%E6%B0%B4%E6%9E%9C%E6%88%90%E7%AF%AE.md) | 数组、哈希表、滑动窗口 | 中等 |
4444
| 1358 | 包含所有三种字符的子字符串数目 | | | |
4545
| 0467 | 环绕字符串中唯一的子字符串 | | | |
4646
| 0220 | [存在重复元素 III](https://leetcode-cn.com/problems/contains-duplicate-iii/) | [Python](https://github.com/itcharge/LeetCode-Py/blob/main/Solutions/0220.%20%E5%AD%98%E5%9C%A8%E9%87%8D%E5%A4%8D%E5%85%83%E7%B4%A0%20III.md) | 排序、有序集合、哈希表 | 中等 |

README.md

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -206,7 +206,7 @@
206206
- ~~[位运算知识](https://github.com/itcharge/LeetCode-Py/blob/main/Contents/09.Algorithm-Base/07.Bit-Operation/01.Bit-Operation.md)~~
207207
- [位运算题目](https://github.com/itcharge/LeetCode-Py/blob/main/Contents/09.Algorithm-Base/07.Bit-Operation/10.Bit-Operation-List.md)
208208

209-
# LeetCode 题解(已完成 608 道)
209+
# LeetCode 题解(已完成 609 道)
210210

211211
| 题号 | 标题 | 题解 | 标签 | 难度 |
212212
| :------ | :------ | :------ | :------ | :------ |
@@ -578,6 +578,7 @@
578578
| 0881 | [救生艇](https://leetcode-cn.com/problems/boats-to-save-people/) | [Python](https://github.com/itcharge/LeetCode-Py/blob/main/Solutions/0881.%20%E6%95%91%E7%94%9F%E8%89%87.md) | 贪心、数组、双指针、排序 | 中等 |
579579
| 0886 | [可能的二分法](https://leetcode-cn.com/problems/possible-bipartition/) | [Python](https://github.com/itcharge/LeetCode-Py/blob/main/Solutions/0886.%20%E5%8F%AF%E8%83%BD%E7%9A%84%E4%BA%8C%E5%88%86%E6%B3%95.md) | 深度优先搜索、广度优先搜索、并查集、图 | 中等 |
580580
| 0897 | [递增顺序搜索树](https://leetcode-cn.com/problems/increasing-order-search-tree/) | [Python](https://github.com/itcharge/LeetCode-Py/blob/main/Solutions/0897.%20%E9%80%92%E5%A2%9E%E9%A1%BA%E5%BA%8F%E6%90%9C%E7%B4%A2%E6%A0%91.md) | 栈、树、深度优先搜索、二叉搜索树、二叉树 | 简单 |
581+
| 0904 | [水果成篮](https://leetcode-cn.com/problems/fruit-into-baskets/) | [Python](https://github.com/itcharge/LeetCode-Py/blob/main/Solutions/0904.%20%E6%B0%B4%E6%9E%9C%E6%88%90%E7%AF%AE.md) | 数组、哈希表、滑动窗口 | 中等 |
581582
| 0908 | [最小差值 I](https://leetcode-cn.com/problems/smallest-range-i/) | [Python](https://github.com/itcharge/LeetCode-Py/blob/main/Solutions/0908.%20%E6%9C%80%E5%B0%8F%E5%B7%AE%E5%80%BC%20I.md) | 数组、数学 | 简单 |
582583
| 0912 | [排序数组](https://leetcode-cn.com/problems/sort-an-array/) | [Python](https://github.com/itcharge/LeetCode-Py/blob/main/Solutions/0912.%20%E6%8E%92%E5%BA%8F%E6%95%B0%E7%BB%84.md) | 数组、分治、桶排序、计数排序、基数排序、排序、堆(优先队列) | 中等 |
583584
| 0918 | [环形子数组的最大和](https://leetcode-cn.com/problems/maximum-sum-circular-subarray/) | [Python](https://github.com/itcharge/LeetCode-Py/blob/main/Solutions/0918.%20%E7%8E%AF%E5%BD%A2%E5%AD%90%E6%95%B0%E7%BB%84%E7%9A%84%E6%9C%80%E5%A4%A7%E5%92%8C.md) | 数组、动态规划 | 中等 |

Solutions/0904. 水果成篮.md

Lines changed: 55 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,55 @@
1+
## [0904. 水果成篮](https://leetcode-cn.com/problems/fruit-into-baskets/)
2+
3+
- 标签:数组、哈希表、滑动窗口
4+
- 难度:中等
5+
6+
## 题目大意
7+
8+
给定一个数组 `fruits`。其中 `fruits[i]` 表示第 `i` 棵树会产生 `fruits[i]` 型水果。
9+
10+
你可以从你选择的任何树开始,然后重复执行以下步骤:
11+
12+
- 把这棵树上的水果放进你的篮子里。如果你做不到,就停下来。
13+
- 移动到当前树右侧的下一棵树。如果右边没有树,就停下来。
14+
- 请注意,在选择一棵树后,你没有任何选择:你必须执行步骤 1,然后执行步骤 2,然后返回步骤 1,然后执行步骤 2,依此类推,直至停止。
15+
16+
你有 `2` 个篮子,每个篮子可以携带任何数量的水果,但你希望每个篮子只携带一种类型的水果。
17+
18+
要求:返回你能收集的水果树的最大总量。
19+
20+
## 解题思路
21+
22+
只有 `2` 个篮子,要求在连续子数组中装最多 `2` 种不同水果。可以理解为维护一个水果种类数为 `2` 的滑动数组,求窗口中最大的水果树数目。具体做法如下:
23+
24+
- 用滑动窗口 `window` 来维护不同种类水果树数目。`window` 为哈希表类型。`ans` 用来维护能收集的水果树的最大总量。设定两个指针:`left``right`,分别指向滑动窗口的左右边界,保证窗口中水果种类数不超过 `2` 种。
25+
- 一开始,`left``right` 都指向 `0`
26+
- 将最右侧数组元素 `fruits[right]` 加入当前窗口 `window` 中,该水果树数目 +1。
27+
- 如果该窗口中该水果树种类多于 `2` 种,即 `len(window) > 2`,则不断右移 `left`,缩小滑动窗口长度,并更新窗口中对应水果树的个数,直到 `len(window) <= 2`
28+
- 维护更新能收集的水果树的最大总量。然后右移 `right`,直到 `right >= len(fruits)` 结束。
29+
- 输出能收集的水果树的最大总量。
30+
31+
## 代码
32+
33+
```Python
34+
class Solution:
35+
def totalFruit(self, fruits: List[int]) -> int:
36+
window = dict()
37+
window_size = 2
38+
ans = 0
39+
left, right = 0, 0
40+
while right < len(fruits):
41+
if fruits[right] in window:
42+
window[fruits[right]] += 1
43+
else:
44+
window[fruits[right]] = 1
45+
46+
while len(window) > window_size:
47+
window[fruits[left]] -= 1
48+
if window[fruits[left]] == 0:
49+
del window[fruits[left]]
50+
left += 1
51+
ans = max(ans, right - left + 1)
52+
right += 1
53+
return ans
54+
```
55+

0 commit comments

Comments
 (0)