diff --git a/Assets/Origins/Categories-List.md b/Assets/Origins/Categories-List.md index 333d9bd1..4b27c52e 100644 --- a/Assets/Origins/Categories-List.md +++ b/Assets/Origins/Categories-List.md @@ -93,7 +93,7 @@ #### 不定长度窗口题目 -###### 0674. 最长连续递增序列、0485. 最大连续 1 的个数、0487. 最大连续1的个数 II、0076. 最小覆盖子串、0718. 最长重复子数组、0209. 长度最小的子数组、0862. 和至少为 K 的最短子数组、1004. 最大连续1的个数 III、1658. 将 x 减到 0 的最小操作数、0424. 替换后的最长重复字符、0003. 无重复字符的最长子串、1695. 删除子数组的最大得分、1208. 尽可能使字符串相等、1493. 删掉一个元素以后全为 1 的最长子数组、0727. 最小窗口子序列、0159. 至多包含两个不同字符的最长子串、0340. 至多包含 K 个不同字符的最长子串、0795. 区间子数组个数、0992. K 个不同整数的子数组、0713. 乘积小于 K 的子数组、0904. 水果成篮、1358. 包含所有三种字符的子字符串数目、0467. 环绕字符串中唯一的子字符串、1438. 绝对差不超过限制的最长连续子数组 +###### 0674. 最长连续递增序列、0485. 最大连续 1 的个数、0487. 最大连续1的个数 II、0076. 最小覆盖子串、0718. 最长重复子数组、0209. 长度最小的子数组、1004. 最大连续1的个数 III、1658. 将 x 减到 0 的最小操作数、0424. 替换后的最长重复字符、0003. 无重复字符的最长子串、1695. 删除子数组的最大得分、1208. 尽可能使字符串相等、1493. 删掉一个元素以后全为 1 的最长子数组、0727. 最小窗口子序列、0159. 至多包含两个不同字符的最长子串、0340. 至多包含 K 个不同字符的最长子串、0795. 区间子数组个数、0992. K 个不同整数的子数组、0713. 乘积小于 K 的子数组、0904. 水果成篮、1358. 包含所有三种字符的子字符串数目、0467. 环绕字符串中唯一的子字符串、1438. 绝对差不超过限制的最长连续子数组 ## 02. 链表 @@ -117,7 +117,7 @@ ### [单调栈](../../Contents/03.Stack/02.Monotone-Stack/02.Monotone-Stack-List.md) -###### 0739. 每日温度、0496. 下一个更大元素 I、0503. 下一个更大元素 II、0901. 股票价格跨度、0084. 柱状图中最大的矩形、0316. 去除重复字母、0042. 接雨水、0085. 最大矩形 +###### 0739. 每日温度、0496. 下一个更大元素 I、0503. 下一个更大元素 II、0901. 股票价格跨度、0084. 柱状图中最大的矩形、0316. 去除重复字母、0042. 接雨水、0085. 最大矩形、0862. 和至少为 K 的最短子数组 ## 04. 队列 diff --git a/Contents/00.Introduction/04.Solutions-List.md b/Contents/00.Introduction/04.Solutions-List.md index ce608f12..07fbc226 100644 --- a/Contents/00.Introduction/04.Solutions-List.md +++ b/Contents/00.Introduction/04.Solutions-List.md @@ -1,4 +1,4 @@ -# LeetCode 题解(已完成 834 道) +# LeetCode 题解(已完成 837 道) | 题号 | 标题 | 题解 | 标签 | 难度 | | :------ | :------ | :------ | :------ | :------ | @@ -447,6 +447,7 @@ | 0852 | [山脉数组的峰顶索引](https://leetcode.cn/problems/peak-index-in-a-mountain-array/) | [Python](https://github.com/itcharge/LeetCode-Py/blob/main/Solutions/0852.%20%E5%B1%B1%E8%84%89%E6%95%B0%E7%BB%84%E7%9A%84%E5%B3%B0%E9%A1%B6%E7%B4%A2%E5%BC%95.md) | 数组、二分查找 | 中等 | | 0860 | [柠檬水找零](https://leetcode.cn/problems/lemonade-change/) | [Python](https://github.com/itcharge/LeetCode-Py/blob/main/Solutions/0860.%20%E6%9F%A0%E6%AA%AC%E6%B0%B4%E6%89%BE%E9%9B%B6.md) | 贪心、数组 | 简单 | | 0861 | [翻转矩阵后的得分](https://leetcode.cn/problems/score-after-flipping-matrix/) | [Python](https://github.com/itcharge/LeetCode-Py/blob/main/Solutions/0861.%20%E7%BF%BB%E8%BD%AC%E7%9F%A9%E9%98%B5%E5%90%8E%E7%9A%84%E5%BE%97%E5%88%86.md) | 贪心、位运算、数组、矩阵 | 中等 | +| 0862 | [和至少为 K 的最短子数组](https://leetcode.cn/problems/shortest-subarray-with-sum-at-least-k/) | [Python](https://github.com/itcharge/LeetCode-Py/blob/main/Solutions/0862.%20%E5%92%8C%E8%87%B3%E5%B0%91%E4%B8%BA%20K%20%E7%9A%84%E6%9C%80%E7%9F%AD%E5%AD%90%E6%95%B0%E7%BB%84.md) | 队列、数组、二分查找、前缀和、滑动窗口、单调队列、堆(优先队列) | 困难 | | 0867 | [转置矩阵](https://leetcode.cn/problems/transpose-matrix/) | [Python](https://github.com/itcharge/LeetCode-Py/blob/main/Solutions/0867.%20%E8%BD%AC%E7%BD%AE%E7%9F%A9%E9%98%B5.md) | 数组、矩阵、模拟 | 简单 | | 0868 | [二进制间距](https://leetcode.cn/problems/binary-gap/) | [Python](https://github.com/itcharge/LeetCode-Py/blob/main/Solutions/0868.%20%E4%BA%8C%E8%BF%9B%E5%88%B6%E9%97%B4%E8%B7%9D.md) | 位运算 | 简单 | | 0872 | [叶子相似的树](https://leetcode.cn/problems/leaf-similar-trees/) | [Python](https://github.com/itcharge/LeetCode-Py/blob/main/Solutions/0872.%20%E5%8F%B6%E5%AD%90%E7%9B%B8%E4%BC%BC%E7%9A%84%E6%A0%91.md) | 树、深度优先搜索、二叉树 | 简单 | @@ -489,6 +490,7 @@ | 0992 | [K 个不同整数的子数组](https://leetcode.cn/problems/subarrays-with-k-different-integers/) | [Python](https://github.com/itcharge/LeetCode-Py/blob/main/Solutions/0992.%20K%20%E4%B8%AA%E4%B8%8D%E5%90%8C%E6%95%B4%E6%95%B0%E7%9A%84%E5%AD%90%E6%95%B0%E7%BB%84.md) | 数组、哈希表、计数、滑动窗口 | 困难 | | 0993 | [二叉树的堂兄弟节点](https://leetcode.cn/problems/cousins-in-binary-tree/) | [Python](https://github.com/itcharge/LeetCode-Py/blob/main/Solutions/0993.%20%E4%BA%8C%E5%8F%89%E6%A0%91%E7%9A%84%E5%A0%82%E5%85%84%E5%BC%9F%E8%8A%82%E7%82%B9.md) | 树、深度优先搜索、广度优先搜索、二叉树 | 简单 | | 0995 | [K 连续位的最小翻转次数](https://leetcode.cn/problems/minimum-number-of-k-consecutive-bit-flips/) | [Python](https://github.com/itcharge/LeetCode-Py/blob/main/Solutions/0995.%20K%20%E8%BF%9E%E7%BB%AD%E4%BD%8D%E7%9A%84%E6%9C%80%E5%B0%8F%E7%BF%BB%E8%BD%AC%E6%AC%A1%E6%95%B0.md) | 位运算、队列、数组、前缀和、滑动窗口 | 困难 | +| 0999 | [可以被一步捕获的棋子数](https://leetcode.cn/problems/available-captures-for-rook/) | [Python](https://github.com/itcharge/LeetCode-Py/blob/main/Solutions/0999.%20%E5%8F%AF%E4%BB%A5%E8%A2%AB%E4%B8%80%E6%AD%A5%E6%8D%95%E8%8E%B7%E7%9A%84%E6%A3%8B%E5%AD%90%E6%95%B0.md) | 数组、矩阵、模拟 | 简单 | | 1000 | [合并石头的最低成本](https://leetcode.cn/problems/minimum-cost-to-merge-stones/) | [Python](https://github.com/itcharge/LeetCode-Py/blob/main/Solutions/1000.%20%E5%90%88%E5%B9%B6%E7%9F%B3%E5%A4%B4%E7%9A%84%E6%9C%80%E4%BD%8E%E6%88%90%E6%9C%AC.md) | 数组、动态规划、前缀和 | 困难 | | 1002 | [查找共用字符](https://leetcode.cn/problems/find-common-characters/) | [Python](https://github.com/itcharge/LeetCode-Py/blob/main/Solutions/1002.%20%E6%9F%A5%E6%89%BE%E5%85%B1%E7%94%A8%E5%AD%97%E7%AC%A6.md) | 数组、哈希表、字符串 | 简单 | | 1004 | [最大连续1的个数 III](https://leetcode.cn/problems/max-consecutive-ones-iii/) | [Python](https://github.com/itcharge/LeetCode-Py/blob/main/Solutions/1004.%20%E6%9C%80%E5%A4%A7%E8%BF%9E%E7%BB%AD1%E7%9A%84%E4%B8%AA%E6%95%B0%20III.md) | 数组、二分查找、前缀和、滑动窗口 | 中等 | @@ -550,6 +552,7 @@ | 1300 | [转变数组后最接近目标值的数组和](https://leetcode.cn/problems/sum-of-mutated-array-closest-to-target/) | [Python](https://github.com/itcharge/LeetCode-Py/blob/main/Solutions/1300.%20%E8%BD%AC%E5%8F%98%E6%95%B0%E7%BB%84%E5%90%8E%E6%9C%80%E6%8E%A5%E8%BF%91%E7%9B%AE%E6%A0%87%E5%80%BC%E7%9A%84%E6%95%B0%E7%BB%84%E5%92%8C.md) | 数组、二分查找、排序 | 中等 | | 1305 | [两棵二叉搜索树中的所有元素](https://leetcode.cn/problems/all-elements-in-two-binary-search-trees/) | [Python](https://github.com/itcharge/LeetCode-Py/blob/main/Solutions/1305.%20%E4%B8%A4%E6%A3%B5%E4%BA%8C%E5%8F%89%E6%90%9C%E7%B4%A2%E6%A0%91%E4%B8%AD%E7%9A%84%E6%89%80%E6%9C%89%E5%85%83%E7%B4%A0.md) | 树、深度优先搜索、二叉搜索树、二叉树、排序 | 中等 | | 1310 | [子数组异或查询](https://leetcode.cn/problems/xor-queries-of-a-subarray/) | [Python](https://github.com/itcharge/LeetCode-Py/blob/main/Solutions/1310.%20%E5%AD%90%E6%95%B0%E7%BB%84%E5%BC%82%E6%88%96%E6%9F%A5%E8%AF%A2.md) | 位运算、数组、前缀和 | 中等 | +| 1313 | [解压缩编码列表](https://leetcode.cn/problems/decompress-run-length-encoded-list/) | [Python](https://github.com/itcharge/LeetCode-Py/blob/main/Solutions/1313.%20%E8%A7%A3%E5%8E%8B%E7%BC%A9%E7%BC%96%E7%A0%81%E5%88%97%E8%A1%A8.md) | 数组 | 简单 | | 1317 | [将整数转换为两个无零整数的和](https://leetcode.cn/problems/convert-integer-to-the-sum-of-two-no-zero-integers/) | [Python](https://github.com/itcharge/LeetCode-Py/blob/main/Solutions/1317.%20%E5%B0%86%E6%95%B4%E6%95%B0%E8%BD%AC%E6%8D%A2%E4%B8%BA%E4%B8%A4%E4%B8%AA%E6%97%A0%E9%9B%B6%E6%95%B4%E6%95%B0%E7%9A%84%E5%92%8C.md) | 数学 | 简单 | | 1319 | [连通网络的操作次数](https://leetcode.cn/problems/number-of-operations-to-make-network-connected/) | [Python](https://github.com/itcharge/LeetCode-Py/blob/main/Solutions/1319.%20%E8%BF%9E%E9%80%9A%E7%BD%91%E7%BB%9C%E7%9A%84%E6%93%8D%E4%BD%9C%E6%AC%A1%E6%95%B0.md) | 深度优先搜索、广度优先搜索、并查集、图 | 中等 | | 1338 | [数组大小减半](https://leetcode.cn/problems/reduce-array-size-to-the-half/) | [Python](https://github.com/itcharge/LeetCode-Py/blob/main/Solutions/1338.%20%E6%95%B0%E7%BB%84%E5%A4%A7%E5%B0%8F%E5%87%8F%E5%8D%8A.md) | 贪心、数组、哈希表、排序、堆(优先队列) | 中等 | diff --git a/Contents/00.Introduction/05.Categories-List.md b/Contents/00.Introduction/05.Categories-List.md index 976ef435..ba0be143 100644 --- a/Contents/00.Introduction/05.Categories-List.md +++ b/Contents/00.Introduction/05.Categories-List.md @@ -246,7 +246,6 @@ | 0076 | [最小覆盖子串](https://leetcode.cn/problems/minimum-window-substring/) | [Python](https://github.com/itcharge/LeetCode-Py/blob/main/Solutions/0076.%20%E6%9C%80%E5%B0%8F%E8%A6%86%E7%9B%96%E5%AD%90%E4%B8%B2.md) | 哈希表、字符串、滑动窗口 | 困难 | | 0718 | [最长重复子数组](https://leetcode.cn/problems/maximum-length-of-repeated-subarray/) | [Python](https://github.com/itcharge/LeetCode-Py/blob/main/Solutions/0718.%20%E6%9C%80%E9%95%BF%E9%87%8D%E5%A4%8D%E5%AD%90%E6%95%B0%E7%BB%84.md) | 数组、二分查找、动态规划、滑动窗口、哈希函数、滚动哈希 | 中等 | | 0209 | [长度最小的子数组](https://leetcode.cn/problems/minimum-size-subarray-sum/) | [Python](https://github.com/itcharge/LeetCode-Py/blob/main/Solutions/0209.%20%E9%95%BF%E5%BA%A6%E6%9C%80%E5%B0%8F%E7%9A%84%E5%AD%90%E6%95%B0%E7%BB%84.md) | 数组、二分查找、前缀和、滑动窗口 | 中等 | -| 0862 | [和至少为 K 的最短子数组](https://leetcode.cn/problems/shortest-subarray-with-sum-at-least-k/) | | 队列、数组、二分查找、前缀和、滑动窗口、单调队列、堆(优先队列) | 困难 | | 1004 | [最大连续1的个数 III](https://leetcode.cn/problems/max-consecutive-ones-iii/) | [Python](https://github.com/itcharge/LeetCode-Py/blob/main/Solutions/1004.%20%E6%9C%80%E5%A4%A7%E8%BF%9E%E7%BB%AD1%E7%9A%84%E4%B8%AA%E6%95%B0%20III.md) | 数组、二分查找、前缀和、滑动窗口 | 中等 | | 1658 | [将 x 减到 0 的最小操作数](https://leetcode.cn/problems/minimum-operations-to-reduce-x-to-zero/) | [Python](https://github.com/itcharge/LeetCode-Py/blob/main/Solutions/1658.%20%E5%B0%86%20x%20%E5%87%8F%E5%88%B0%200%20%E7%9A%84%E6%9C%80%E5%B0%8F%E6%93%8D%E4%BD%9C%E6%95%B0.md) | 数组、哈希表、二分查找、前缀和、滑动窗口 | 中等 | | 0424 | [替换后的最长重复字符](https://leetcode.cn/problems/longest-repeating-character-replacement/) | [Python](https://github.com/itcharge/LeetCode-Py/blob/main/Solutions/0424.%20%E6%9B%BF%E6%8D%A2%E5%90%8E%E7%9A%84%E6%9C%80%E9%95%BF%E9%87%8D%E5%A4%8D%E5%AD%97%E7%AC%A6.md) | 哈希表、字符串、滑动窗口 | 中等 | @@ -339,6 +338,7 @@ | 0316 | [去除重复字母](https://leetcode.cn/problems/remove-duplicate-letters/) | [Python](https://github.com/itcharge/LeetCode-Py/blob/main/Solutions/0316.%20%E5%8E%BB%E9%99%A4%E9%87%8D%E5%A4%8D%E5%AD%97%E6%AF%8D.md) | 栈、贪心、字符串、单调栈 | 中等 | | 0042 | [接雨水](https://leetcode.cn/problems/trapping-rain-water/) | [Python](https://github.com/itcharge/LeetCode-Py/blob/main/Solutions/0042.%20%E6%8E%A5%E9%9B%A8%E6%B0%B4.md) | 栈、数组、双指针、动态规划、单调栈 | 困难 | | 0085 | [最大矩形](https://leetcode.cn/problems/maximal-rectangle/) | | 栈、数组、动态规划、矩阵、单调栈 | 困难 | +| 0862 | [和至少为 K 的最短子数组](https://leetcode.cn/problems/shortest-subarray-with-sum-at-least-k/) | [Python](https://github.com/itcharge/LeetCode-Py/blob/main/Solutions/0862.%20%E5%92%8C%E8%87%B3%E5%B0%91%E4%B8%BA%20K%20%E7%9A%84%E6%9C%80%E7%9F%AD%E5%AD%90%E6%95%B0%E7%BB%84.md) | 队列、数组、二分查找、前缀和、滑动窗口、单调队列、堆(优先队列) | 困难 | ## 04. 队列 diff --git a/Contents/00.Introduction/07.Interview-200-List.md b/Contents/00.Introduction/07.Interview-200-List.md index 2c2f0798..1a597a0b 100644 --- a/Contents/00.Introduction/07.Interview-200-List.md +++ b/Contents/00.Introduction/07.Interview-200-List.md @@ -166,7 +166,7 @@ | 0076 | [最小覆盖子串](https://leetcode.cn/problems/minimum-window-substring/) | [Python](https://github.com/itcharge/LeetCode-Py/blob/main/Solutions/0076.%20%E6%9C%80%E5%B0%8F%E8%A6%86%E7%9B%96%E5%AD%90%E4%B8%B2.md) | 哈希表、字符串、滑动窗口 | 困难 | | 0718 | [最长重复子数组](https://leetcode.cn/problems/maximum-length-of-repeated-subarray/) | [Python](https://github.com/itcharge/LeetCode-Py/blob/main/Solutions/0718.%20%E6%9C%80%E9%95%BF%E9%87%8D%E5%A4%8D%E5%AD%90%E6%95%B0%E7%BB%84.md) | 数组、二分查找、动态规划、滑动窗口、哈希函数、滚动哈希 | 中等 | | 0209 | [长度最小的子数组](https://leetcode.cn/problems/minimum-size-subarray-sum/) | [Python](https://github.com/itcharge/LeetCode-Py/blob/main/Solutions/0209.%20%E9%95%BF%E5%BA%A6%E6%9C%80%E5%B0%8F%E7%9A%84%E5%AD%90%E6%95%B0%E7%BB%84.md) | 数组、二分查找、前缀和、滑动窗口 | 中等 | -| 0862 | [和至少为 K 的最短子数组](https://leetcode.cn/problems/shortest-subarray-with-sum-at-least-k/) | | 队列、数组、二分查找、前缀和、滑动窗口、单调队列、堆(优先队列) | 困难 | +| 0862 | [和至少为 K 的最短子数组](https://leetcode.cn/problems/shortest-subarray-with-sum-at-least-k/) | [Python](https://github.com/itcharge/LeetCode-Py/blob/main/Solutions/0862.%20%E5%92%8C%E8%87%B3%E5%B0%91%E4%B8%BA%20K%20%E7%9A%84%E6%9C%80%E7%9F%AD%E5%AD%90%E6%95%B0%E7%BB%84.md) | 队列、数组、二分查找、前缀和、滑动窗口、单调队列、堆(优先队列) | 困难 | | 1004 | [最大连续1的个数 III](https://leetcode.cn/problems/max-consecutive-ones-iii/) | [Python](https://github.com/itcharge/LeetCode-Py/blob/main/Solutions/1004.%20%E6%9C%80%E5%A4%A7%E8%BF%9E%E7%BB%AD1%E7%9A%84%E4%B8%AA%E6%95%B0%20III.md) | 数组、二分查找、前缀和、滑动窗口 | 中等 | ## 02. 链表 diff --git a/Contents/01.Array/05.Array-Sliding-Window/02.Array-Sliding-Window-List.md b/Contents/01.Array/05.Array-Sliding-Window/02.Array-Sliding-Window-List.md index 12e27653..9d613a70 100644 --- a/Contents/01.Array/05.Array-Sliding-Window/02.Array-Sliding-Window-List.md +++ b/Contents/01.Array/05.Array-Sliding-Window/02.Array-Sliding-Window-List.md @@ -30,7 +30,6 @@ | 0076 | [最小覆盖子串](https://leetcode.cn/problems/minimum-window-substring/) | [Python](https://github.com/itcharge/LeetCode-Py/blob/main/Solutions/0076.%20%E6%9C%80%E5%B0%8F%E8%A6%86%E7%9B%96%E5%AD%90%E4%B8%B2.md) | 哈希表、字符串、滑动窗口 | 困难 | | 0718 | [最长重复子数组](https://leetcode.cn/problems/maximum-length-of-repeated-subarray/) | [Python](https://github.com/itcharge/LeetCode-Py/blob/main/Solutions/0718.%20%E6%9C%80%E9%95%BF%E9%87%8D%E5%A4%8D%E5%AD%90%E6%95%B0%E7%BB%84.md) | 数组、二分查找、动态规划、滑动窗口、哈希函数、滚动哈希 | 中等 | | 0209 | [长度最小的子数组](https://leetcode.cn/problems/minimum-size-subarray-sum/) | [Python](https://github.com/itcharge/LeetCode-Py/blob/main/Solutions/0209.%20%E9%95%BF%E5%BA%A6%E6%9C%80%E5%B0%8F%E7%9A%84%E5%AD%90%E6%95%B0%E7%BB%84.md) | 数组、二分查找、前缀和、滑动窗口 | 中等 | -| 0862 | [和至少为 K 的最短子数组](https://leetcode.cn/problems/shortest-subarray-with-sum-at-least-k/) | | 队列、数组、二分查找、前缀和、滑动窗口、单调队列、堆(优先队列) | 困难 | | 1004 | [最大连续1的个数 III](https://leetcode.cn/problems/max-consecutive-ones-iii/) | [Python](https://github.com/itcharge/LeetCode-Py/blob/main/Solutions/1004.%20%E6%9C%80%E5%A4%A7%E8%BF%9E%E7%BB%AD1%E7%9A%84%E4%B8%AA%E6%95%B0%20III.md) | 数组、二分查找、前缀和、滑动窗口 | 中等 | | 1658 | [将 x 减到 0 的最小操作数](https://leetcode.cn/problems/minimum-operations-to-reduce-x-to-zero/) | [Python](https://github.com/itcharge/LeetCode-Py/blob/main/Solutions/1658.%20%E5%B0%86%20x%20%E5%87%8F%E5%88%B0%200%20%E7%9A%84%E6%9C%80%E5%B0%8F%E6%93%8D%E4%BD%9C%E6%95%B0.md) | 数组、哈希表、二分查找、前缀和、滑动窗口 | 中等 | | 0424 | [替换后的最长重复字符](https://leetcode.cn/problems/longest-repeating-character-replacement/) | [Python](https://github.com/itcharge/LeetCode-Py/blob/main/Solutions/0424.%20%E6%9B%BF%E6%8D%A2%E5%90%8E%E7%9A%84%E6%9C%80%E9%95%BF%E9%87%8D%E5%A4%8D%E5%AD%97%E7%AC%A6.md) | 哈希表、字符串、滑动窗口 | 中等 | diff --git a/Contents/03.Stack/02.Monotone-Stack/02.Monotone-Stack-List.md b/Contents/03.Stack/02.Monotone-Stack/02.Monotone-Stack-List.md index bbed1109..acab1306 100644 --- a/Contents/03.Stack/02.Monotone-Stack/02.Monotone-Stack-List.md +++ b/Contents/03.Stack/02.Monotone-Stack/02.Monotone-Stack-List.md @@ -10,4 +10,5 @@ | 0316 | [去除重复字母](https://leetcode.cn/problems/remove-duplicate-letters/) | [Python](https://github.com/itcharge/LeetCode-Py/blob/main/Solutions/0316.%20%E5%8E%BB%E9%99%A4%E9%87%8D%E5%A4%8D%E5%AD%97%E6%AF%8D.md) | 栈、贪心、字符串、单调栈 | 中等 | | 0042 | [接雨水](https://leetcode.cn/problems/trapping-rain-water/) | [Python](https://github.com/itcharge/LeetCode-Py/blob/main/Solutions/0042.%20%E6%8E%A5%E9%9B%A8%E6%B0%B4.md) | 栈、数组、双指针、动态规划、单调栈 | 困难 | | 0085 | [最大矩形](https://leetcode.cn/problems/maximal-rectangle/) | | 栈、数组、动态规划、矩阵、单调栈 | 困难 | +| 0862 | [和至少为 K 的最短子数组](https://leetcode.cn/problems/shortest-subarray-with-sum-at-least-k/) | [Python](https://github.com/itcharge/LeetCode-Py/blob/main/Solutions/0862.%20%E5%92%8C%E8%87%B3%E5%B0%91%E4%B8%BA%20K%20%E7%9A%84%E6%9C%80%E7%9F%AD%E5%AD%90%E6%95%B0%E7%BB%84.md) | 队列、数组、二分查找、前缀和、滑动窗口、单调队列、堆(优先队列) | 困难 | diff --git a/README.md b/README.md index 2b3b7a9b..749c674c 100644 --- a/README.md +++ b/README.md @@ -255,4 +255,4 @@ - [动态规划优化题目](./Contents/10.Dynamic-Programming/11.DP-Optimization/04.DP-Optimization-List.md) ## 11. 附加内容 -## [12. LeetCode 题解(已完成 834 道)](./Contents/00.Introduction/04.Solutions-List.md) \ No newline at end of file +## [12. LeetCode 题解(已完成 837 道)](./Contents/00.Introduction/04.Solutions-List.md) \ No newline at end of file diff --git "a/Solutions/0424. \346\233\277\346\215\242\345\220\216\347\232\204\346\234\200\351\225\277\351\207\215\345\244\215\345\255\227\347\254\246.md" "b/Solutions/0424. \346\233\277\346\215\242\345\220\216\347\232\204\346\234\200\351\225\277\351\207\215\345\244\215\345\255\227\347\254\246.md" index edf2299f..4668e405 100644 --- "a/Solutions/0424. \346\233\277\346\215\242\345\220\216\347\232\204\346\234\200\351\225\277\351\207\215\345\244\215\345\255\227\347\254\246.md" +++ "b/Solutions/0424. \346\233\277\346\215\242\345\220\216\347\232\204\346\234\200\351\225\277\351\207\215\345\244\215\345\255\227\347\254\246.md" @@ -9,7 +9,36 @@ ## 题目大意 -给定一个仅由大写英文字母组成的字符串 s,以及一个整数 k。可以将任意位置上的字符替换成另外的大写字母,最多可替换 k 次。再进行上述操作后,找到包含重复字母的最长子串长度。 +**描述**:给定一个仅由大写英文字母组成的字符串 $s$,以及一个整数 $k$。可以将任意位置上的字符替换成另外的大写字母,最多可替换 $k$ 次。 + +**要求**:在进行上述操作后,找到包含重复字母的最长子串长度。 + +**说明**: + +- $1 \le s.length \le 10^5$。 +- $s$ 仅由大写英文字母组成。 +- $0 \le k \le s.length$。 + +**示例**: + +- 示例 1: + +```python +输入:s = "ABAB", k = 2 +输出:4 +解释:用两个'A'替换为两个'B',反之亦然。 +``` + +- 示例 2: + +```python +输入:s = "AABABBA", k = 1 +输出:4 +解释: +将中间的一个'A'替换为'B',字符串变为 "AABBBBA"。 +子串 "BBBB" 有最长重复字母, 答案为 4。 +可能存在其他的方法来得到同样的结果。 +``` ## 解题思路 @@ -20,13 +49,13 @@ 但是这种暴力求法中,枚举子串的时间复杂度为 $O(n^2)$,统计出现次数最多的字符和替换字符时间复杂度为 $0(n)$,且两者属于平行处理,总体下来的时间复杂度为 $O(n^3)$。这样做会超时。 -下面采用滑动窗口来做。 +### 思路 1:滑动窗口 -- 使用 counts 数组来统计字母频数。使用 left、right 双指针分别指向滑动窗口的首尾位置,使用 max_count 来维护最长子串的长度。 -- 不断右移 right 指针,增加滑动窗口的长度。 -- 对于当前滑动窗口的子串,如果当前窗口的间距 > 当前出现最大次数的字符的次数 + k 时,意味着替换 k 次仍不能使当前窗口中的字符全变为相同字符,则此时应该将左边界右移,同时将原先左边界的字符频次减少。 +1. 使用 counts 数组来统计字母频数。使用 left、right 双指针分别指向滑动窗口的首尾位置,使用 max_count 来维护最长子串的长度。 +2. 不断右移 right 指针,增加滑动窗口的长度。 +3. 对于当前滑动窗口的子串,如果当前窗口的间距 > 当前出现最大次数的字符的次数 + k 时,意味着替换 k 次仍不能使当前窗口中的字符全变为相同字符,则此时应该将左边界右移,同时将原先左边界的字符频次减少。 -## 代码 +### 思路 1:代码 ```python class Solution: @@ -47,3 +76,8 @@ class Solution: return right - left ``` +### 思路 1:复杂度分析 + +- **时间复杂度**:$O(n)$,其中 $n$ 为字符串的长度。 +- **空间复杂度**:$O(|\sum|)$,其中 $\sum$ 是字符集,本题中 $| \sum | = 26$。 + diff --git "a/Solutions/0999. \345\217\257\344\273\245\350\242\253\344\270\200\346\255\245\346\215\225\350\216\267\347\232\204\346\243\213\345\255\220\346\225\260.md" "b/Solutions/0999. \345\217\257\344\273\245\350\242\253\344\270\200\346\255\245\346\215\225\350\216\267\347\232\204\346\243\213\345\255\220\346\225\260.md" new file mode 100644 index 00000000..5394446c --- /dev/null +++ "b/Solutions/0999. \345\217\257\344\273\245\350\242\253\344\270\200\346\255\245\346\215\225\350\216\267\347\232\204\346\243\213\345\255\220\346\225\260.md" @@ -0,0 +1,93 @@ +# [0999. 可以被一步捕获的棋子数](https://leetcode.cn/problems/available-captures-for-rook/) + +- 标签:数组、矩阵、模拟 +- 难度:简单 + +## 题目链接 + +- [0999. 可以被一步捕获的棋子数 - 力扣](https://leetcode.cn/problems/available-captures-for-rook/) + +## 题目大意 + +**描述**:在一个 $8 \times 8$ 的棋盘上,有一个白色的车(Rook),用字符 `'R'` 表示。棋盘上还可能存在空方块,白色的象(Bishop)以及黑色的卒(pawn),分别用字符 `'.'`,`'B'` 和 `'p'` 表示。不难看出,大写字符表示的是白棋,小写字符表示的是黑棋。 + +**要求**:你现在可以控制车移动一次,请你统计有多少敌方的卒处于你的捕获范围内(即,可以被一步捕获的棋子数)。 + +**说明**: + +- 车按国际象棋中的规则移动。东,西,南,北四个基本方向任选其一,然后一直向选定的方向移动,直到满足下列四个条件之一: + - 棋手选择主动停下来。 + - 棋子因到达棋盘的边缘而停下。 + - 棋子移动到某一方格来捕获位于该方格上敌方(黑色)的卒,停在该方格内。 + - 车不能进入/越过已经放有其他友方棋子(白色的象)的方格,停在友方棋子前。 + +- $board.length == board[i].length == 8$ +- $board[i][j]$ 可以是 `'R'`,`'.'`,`'B'` 或 `'p'`。 +- 只有一个格子上存在 $board[i][j] == 'R'$。 + +**示例**: + +- 示例 1: + +![](https://assets.leetcode-cn.com/aliyun-lc-upload/uploads/2019/02/23/1253_example_1_improved.PNG) + +```python +输入:[[".",".",".",".",".",".",".","."],[".",".",".","p",".",".",".","."],[".",".",".","R",".",".",".","p"],[".",".",".",".",".",".",".","."],[".",".",".",".",".",".",".","."],[".",".",".","p",".",".",".","."],[".",".",".",".",".",".",".","."],[".",".",".",".",".",".",".","."]] +输出:3 +解释:在本例中,车能够捕获所有的卒。 +``` + +- 示例 2: + +![img](https://assets.leetcode-cn.com/aliyun-lc-upload/uploads/2019/02/23/1253_example_2_improved.PNG) + +```python +输入:[[".",".",".",".",".",".",".","."],[".","p","p","p","p","p",".","."],[".","p","p","B","p","p",".","."],[".","p","B","R","B","p",".","."],[".","p","p","B","p","p",".","."],[".","p","p","p","p","p",".","."],[".",".",".",".",".",".",".","."],[".",".",".",".",".",".",".","."]] +输出:0 +解释:象阻止了车捕获任何卒。 +``` + +## 解题思路 + +### 思路 1:模拟 + +1. 双重循环遍历确定白色车的位置 $(pos\underline{}i,poss\underline{}j)$。 +2. 让车向上、下、左、右四个方向进行移动,直到超出边界 / 碰到白色象 / 碰到卒为止。使用计数器 $cnt$ 记录捕获的卒的数量。 +3. 返回答案 $cnt$。 + +### 思路 1:代码 + +```Python +class Solution: + def numRookCaptures(self, board: List[List[str]]) -> int: + directions = {(1, 0), (-1, 0), (0, 1), (0, -1)} + pos_i, pos_j = -1, -1 + for i in range(len(board)): + if pos_i != -1 and pos_j != -1: + break + for j in range(len(board[i])): + if board[i][j] == 'R': + pos_i, pos_j = i, j + break + + cnt = 0 + for direction in directions: + setp = 0 + while True: + new_i = pos_i + setp * direction[0] + new_j = pos_j + setp * direction[1] + if new_i < 0 or new_i >= 8 or new_j < 0 or new_j >= 8 or board[new_i][new_j] == 'B': + break + if board[new_i][new_j] == 'p': + cnt += 1 + break + setp += 1 + + return cnt +``` + +### 思路 1:复杂度分析 + +- **时间复杂度**:$O(n^2)$,其中 $n$ 为棋盘的边长。 +- **空间复杂度**:$O(1)$。 + diff --git "a/Solutions/1313. \350\247\243\345\216\213\347\274\251\347\274\226\347\240\201\345\210\227\350\241\250.md" "b/Solutions/1313. \350\247\243\345\216\213\347\274\251\347\274\226\347\240\201\345\210\227\350\241\250.md" new file mode 100644 index 00000000..06e73ebf --- /dev/null +++ "b/Solutions/1313. \350\247\243\345\216\213\347\274\251\347\274\226\347\240\201\345\210\227\350\241\250.md" @@ -0,0 +1,69 @@ +# [1313. 解压缩编码列表](https://leetcode.cn/problems/decompress-run-length-encoded-list/) + +- 标签:数组 +- 难度:简单 + +## 题目链接 + +- [1313. 解压缩编码列表 - 力扣](https://leetcode.cn/problems/decompress-run-length-encoded-list/) + +## 题目大意 + +**描述**:给定一个以行程长度编码压缩的整数列表 $nums$。 + +考虑每对相邻的两个元素 $[freq, val] = [nums[2 \times i], nums[2 \times i + 1]]$ (其中 $i \ge 0$ ),每一对都表示解压后子列表中有 $freq$ 个值为 $val$ 的元素,你需要从左到右连接所有子列表以生成解压后的列表。 + +**要求**:返回解压后的列表。 + +**说明**: + +- $2 \le nums.length \le 100$。 +- $nums.length \mod 2 == 0$。 +- $1 \le nums[i] \le 100$。 + +**示例**: + +- 示例 1: + +```python +输入:nums = [1,2,3,4] +输出:[2,4,4,4] +解释:第一对 [1,2] 代表着 2 的出现频次为 1,所以生成数组 [2]。 +第二对 [3,4] 代表着 4 的出现频次为 3,所以生成数组 [4,4,4]。 +最后将它们串联到一起 [2] + [4,4,4] = [2,4,4,4]。 +``` + +- 示例 2: + +```python +输入:nums = [1,1,2,3] +输出:[1,3,3] +``` + +## 解题思路 + +### 思路 1:模拟 + +1. 以步长为 $2$,遍历数组 $nums$。 +2. 对于遍历到的元素 $nums[i]$、$nnums[i + 1]$,将 $nums[i]$ 个 $nums[i + 1]$ 存入答案数组中。 +3. 返回答案数组。 + +### 思路 1:代码 + +```Python +class Solution: + def decompressRLElist(self, nums: List[int]) -> List[int]: + res = [] + for i in range(0, len(nums), 2): + cnts = nums[i] + for cnt in range(cnts): + res.append(nums[i + 1]) + + return res +``` + +### 思路 1:复杂度分析 + +- **时间复杂度**:$O(n + s)$,其中 $n$ 为数组 $nums$ 的长度,$s$ 是数组 $nums$ 中所有偶数下标对应元素之和。 +- **空间复杂度**:$O(s)$。 + diff --git "a/Solutions/1658. \345\260\206 x \345\207\217\345\210\260 0 \347\232\204\346\234\200\345\260\217\346\223\215\344\275\234\346\225\260.md" "b/Solutions/1658. \345\260\206 x \345\207\217\345\210\260 0 \347\232\204\346\234\200\345\260\217\346\223\215\344\275\234\346\225\260.md" index 78af2544..4301bd88 100644 --- "a/Solutions/1658. \345\260\206 x \345\207\217\345\210\260 0 \347\232\204\346\234\200\345\260\217\346\223\215\344\275\234\346\225\260.md" +++ "b/Solutions/1658. \345\260\206 x \345\207\217\345\210\260 0 \347\232\204\346\234\200\345\260\217\346\223\215\344\275\234\346\225\260.md" @@ -9,25 +9,51 @@ ## 题目大意 -给你一个整数数组 `nums` 和一个整数 `x` 。每一次操作时,你应当移除数组 `nums` 最左边或最右边的元素,然后从 `x` 中减去该元素的值。请注意,需要修改数组以供接下来的操作使用。 +**描述**:给定一个整数数组 $nums$ 和一个整数 $x$ 。每一次操作时,你应当移除数组 $nums$ 最左边或最右边的元素,然后从 $x$ 中减去该元素的值。请注意,需要修改数组以供接下来的操作使用。 -要求:如果可以将 `x` 恰好减到 `0`,返回最小操作数;否则,返回 `-1`。 +**要求**:如果可以将 $x$ 恰好减到 $0$,返回最小操作数;否则,返回 $-1$。 + +**说明**: + +- $1 \le nums.length \le 10^5$。 +- $1 \le nums[i] \le 10^4$。 +- $1 \le x \le 10^9$。 + +**示例**: + +- 示例 1: + +```python +输入:nums = [1,1,4,2,3], x = 5 +输出:2 +解释:最佳解决方案是移除后两个元素,将 x 减到 0。 +``` + +- 示例 2: + +```python +输入:nums = [3,2,20,1,1,3], x = 10 +输出:5 +解释:最佳解决方案是移除后三个元素和前两个元素(总共 5 次操作),将 x 减到 0。 +``` ## 解题思路 -将 `x` 减到 `0` 的最小操作数可以转换为求和等于 `sum(nums) - x` 的最长连续子数组长度。我们可以维护一个区间和为 `sum(nums) - x` 的滑动窗口,求出最长的窗口长度。具体做法如下: +### 思路 1:滑动窗口 -令 `target = sum(nums) - x`,使用 `max_len` 维护和等于 `target` 的最长连续子数组长度。然后用滑动窗口 `window_sum` 来记录连续子数组的和,设定两个指针:`left`、`right`,分别指向滑动窗口的左右边界,保证窗口中的和刚好等于 `target`。 +将 $x$ 减到 $0$ 的最小操作数可以转换为求和等于 $sum(nums) - x$ 的最长连续子数组长度。我们可以维护一个区间和为 $sum(nums) - x$ 的滑动窗口,求出最长的窗口长度。具体做法如下: -- 一开始,`left`、`right` 都指向 `0`。 -- 向右移动 `right`,将最右侧元素加入当前窗口和 `window_sum` 中。 -- 如果 `window_sum > target`,则不断右移 `left`,缩小滑动窗口长度,并更新窗口和的最小值,直到 `window_sum <= target`。 -- 如果 `window_sum == target`,则更新最长连续子数组长度。 -- 然后继续右移 `right`,直到 `right >= len(nums)` 结束。 -- 输出 `len(nums) - max_len` 作为答案。 +令 `target = sum(nums) - x`,使用 $max\underline{}len$ 维护和等于 $target$ 的最长连续子数组长度。然后用滑动窗口 $window\underline{}sum$ 来记录连续子数组的和,设定两个指针:$left$、$right$,分别指向滑动窗口的左右边界,保证窗口中的和刚好等于 $target$。 + +- 一开始,$left$、$right$ 都指向 $0$。 +- 向右移动 $right$,将最右侧元素加入当前窗口和 $window\underline{}sum$ 中。 +- 如果 $window\underline{}sum > target$,则不断右移 $left$,缩小滑动窗口长度,并更新窗口和的最小值,直到 $window\underline{}sum \le target$。 +- 如果 $window\underline{}sum == target$,则更新最长连续子数组长度。 +- 然后继续右移 $right$,直到 $right \ge len(nums)$ 结束。 +- 输出 $len(nums) - max\underline{}len$ 作为答案。 - 注意判断题目中的特殊情况。 -## 代码 +### 思路 1:代码 ```python class Solution: @@ -54,3 +80,8 @@ class Solution: return len(nums) - max_len if max_len != float('-inf') else -1 ``` +### 思路 1:复杂度分析 + +- **时间复杂度**:$O(n)$,其中 $n$ 为数组 $nums$ 的长度。 +- **空间复杂度**:$O(1)$。 + diff --git "a/Solutions/1695. \345\210\240\351\231\244\345\255\220\346\225\260\347\273\204\347\232\204\346\234\200\345\244\247\345\276\227\345\210\206.md" "b/Solutions/1695. \345\210\240\351\231\244\345\255\220\346\225\260\347\273\204\347\232\204\346\234\200\345\244\247\345\276\227\345\210\206.md" index 592590ee..e9119f47 100644 --- "a/Solutions/1695. \345\210\240\351\231\244\345\255\220\346\225\260\347\273\204\347\232\204\346\234\200\345\244\247\345\276\227\345\210\206.md" +++ "b/Solutions/1695. \345\210\240\351\231\244\345\255\220\346\225\260\347\273\204\347\232\204\346\234\200\345\244\247\345\276\227\345\210\206.md" @@ -9,23 +9,49 @@ ## 题目大意 -给你一个正整数数组 `nums`,从中删除一个含有若干不同元素的子数组。删除子数组的「得分」就是子数组各元素之和 。 +**描述**:给定一个正整数数组 $nums$,从中删除一个含有若干不同元素的子数组。删除子数组的「得分」就是子数组各元素之和 。 -要求:返回只删除一个子数组可获得的最大得分 。 +**要求**:返回只删除一个子数组可获得的最大得分。 + +**说明**: + +- **子数组**:如果数组 $b$ 是数组 $a$ 的一个连续子序列,即如果它等于 $a[l],a[l+1],...,a[r]$ ,那么它就是 $a$ 的一个子数组。 +- $1 \le nums.length \le 10^5$。 +- $1 \le nums[i] \le 10^4$。 + +**示例**: + +- 示例 1: + +```python +输入:nums = [4,2,4,5,6] +输出:17 +解释:最优子数组是 [2,4,5,6] +``` + +- 示例 2: + +```python +输入:nums = [5,2,1,2,5,2,1,2,5] +输出:8 +解释:最优子数组是 [5,2,1] 或 [1,2,5] +``` ## 解题思路 +### 思路 1:滑动窗口 + 题目要求的是含有不同元素的连续子数组最大和,我们可以用滑动窗口来做,维护一个不包含重复元素的滑动窗口,计算最大的窗口和。具体方法如下: -- 用滑动窗口 `window` 来记录不重复的元素个数,`window` 为哈希表类型。用 `window_sum` 来记录窗口内子数组元素和,`ans` 用来维护最大子数组和。设定两个指针:`left`、`right`,分别指向滑动窗口的左右边界,保证窗口中没有重复元素。 +- 用滑动窗口 $window$ 来记录不重复的元素个数,$window$ 为哈希表类型。用 $window\underline{}sum$ 来记录窗口内子数组元素和,$ans$ 用来维护最大子数组和。设定两个指针:$left$、$right$,分别指向滑动窗口的左右边界,保证窗口中没有重复元素。 -- 一开始,`left`、`right` 都指向 `0`。 -- 将最右侧数组元素 `nums[right]` 加入当前窗口 `window` 中,记录该元素个数。 -- 如果该窗口中该元素的个数多于 `1` 个,即 `window[s[right]] > 1`,则不断右移 `left`,缩小滑动窗口长度,并更新窗口中对应元素的个数,直到 `window[s[right]] <= 1`。 -- 维护更新无重复元素的最大子数组和。然后右移 `right`,直到 `right >= len(nums)` 结束。 +- 一开始,$left$、$right$ 都指向 $0$。 +- 将最右侧数组元素 $nums[right]$ 加入当前窗口 $window$ 中,记录该元素个数。 +- 如果该窗口中该元素的个数多于 $1$ 个,即 $window[s[right]] > 1$,则不断右移 $left$,缩小滑动窗口长度,并更新窗口中对应元素的个数,直到 $window[s[right]] \le 1$。 +- 维护更新无重复元素的最大子数组和。然后右移 $right$,直到 $right \ge len(nums)$ 结束。 - 输出无重复元素的最大子数组和。 -## 代码 +### 思路 1:代码 ```python class Solution: @@ -50,3 +76,8 @@ class Solution: return ans ``` +### 思路 1:复杂度分析 + +- **时间复杂度**:$O(n)$,其中 $n$ 为数组 $nums$ 的长度。 +- **空间复杂度**:$O(n)$。 +