From f4a1c9d52045b7dcd8afa83f7085b8193a9d2835 Mon Sep 17 00:00:00 2001 From: AC_Oier Date: Tue, 31 May 2022 16:59:39 +0800 Subject: [PATCH] =?UTF-8?q?=E2=9C=A8feat:=20add=20209?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- "Index/\344\272\214\345\210\206.md" | 1 + .../\345\211\215\347\274\200\345\222\214.md" | 1 + ...10\344\270\255\347\255\211\357\274\211.md" | 84 +++++++++++++++++++ ...10\344\270\255\347\255\211\357\274\211.md" | 2 +- 4 files changed, 87 insertions(+), 1 deletion(-) create mode 100644 "LeetCode/201-210/209. \351\225\277\345\272\246\346\234\200\345\260\217\347\232\204\345\255\220\346\225\260\347\273\204\357\274\210\344\270\255\347\255\211\357\274\211.md" diff --git "a/Index/\344\272\214\345\210\206.md" "b/Index/\344\272\214\345\210\206.md" index f4b7e23e..6efaa9db 100644 --- "a/Index/\344\272\214\345\210\206.md" +++ "b/Index/\344\272\214\345\210\206.md" @@ -10,6 +10,7 @@ | [153. 寻找旋转排序数组中的最小值](https://leetcode-cn.com/problems/find-minimum-in-rotated-sorted-array/) | [LeetCode 题解链接](https://leetcode-cn.com/problems/find-minimum-in-rotated-sorted-array/solution/gong-shui-san-xie-yan-ge-olognyi-qi-kan-6d969/) | 中等 | 🤩🤩🤩 | | [154. 寻找旋转排序数组中的最小值 II](https://leetcode-cn.com/problems/find-minimum-in-rotated-sorted-array-ii/) | [LeetCode 题解链接](https://leetcode-cn.com/problems/find-minimum-in-rotated-sorted-array-ii/solution/gong-shui-san-xie-xiang-jie-wei-he-yuan-7xbty/) | 困难 | 🤩🤩🤩 | | [162. 寻找峰值](https://leetcode-cn.com/problems/find-peak-element/) | [LeetCode 题解链接](https://leetcode-cn.com/problems/find-peak-element/solution/gong-shui-san-xie-noxiang-xin-ke-xue-xi-qva7v/) | 中等 | 🤩🤩🤩🤩🤩 | +| [209. 长度最小的子数组](https://leetcode.cn/problems/minimum-size-subarray-sum/) | [LeetCode 题解链接](https://leetcode.cn/problems/minimum-size-subarray-sum/solution/by-ac_oier-c5jm/) | 中等 | 🤩🤩🤩🤩🤩 | | [220. 存在重复元素 III](https://leetcode-cn.com/problems/contains-duplicate-iii/) | [LeetCode 题解链接](https://leetcode-cn.com/problems/contains-duplicate-iii/solution/gong-shui-san-xie-yi-ti-shuang-jie-hua-d-dlnv/) | 中等 | 🤩🤩🤩 | | [240. 搜索二维矩阵 II](https://leetcode-cn.com/problems/search-a-2d-matrix-ii/) | [LeetCode 题解链接](https://leetcode-cn.com/problems/search-a-2d-matrix-ii/solution/gong-shui-san-xie-yi-ti-shuang-jie-er-fe-y1ns/) | 中等 | 🤩🤩🤩🤩 | | [274. H 指数](https://leetcode-cn.com/problems/h-index/) | [LeetCode 题解链接](https://leetcode-cn.com/problems/h-index/solution/gong-shui-san-xie-li-yong-er-duan-xing-z-1jxw/) | 中等 | 🤩🤩🤩 | diff --git "a/Index/\345\211\215\347\274\200\345\222\214.md" "b/Index/\345\211\215\347\274\200\345\222\214.md" index c9af4431..ffaf52d8 100644 --- "a/Index/\345\211\215\347\274\200\345\222\214.md" +++ "b/Index/\345\211\215\347\274\200\345\222\214.md" @@ -1,6 +1,7 @@ | 题目 | 题解 | 难度 | 推荐指数 | | ------------------------------------------------------------ | ------------------------------------------------------------ | ---- | -------- | | [187. 重复的DNA序列](https://leetcode-cn.com/problems/repeated-dna-sequences/) | [LeetCode 题解链接](https://leetcode-cn.com/problems/repeated-dna-sequences/solution/gong-shui-san-xie-yi-ti-shuang-jie-hua-d-30pg/) | 中等 | 🤩🤩🤩🤩 | +| [209. 长度最小的子数组](https://leetcode.cn/problems/minimum-size-subarray-sum/) | [LeetCode 题解链接](https://leetcode.cn/problems/minimum-size-subarray-sum/solution/by-ac_oier-c5jm/) | 中等 | 🤩🤩🤩🤩🤩 | | [304. 二维区域和检索 - 矩阵不可变](https://leetcode-cn.com/problems/range-sum-query-2d-immutable/) | [LeetCode 题解链接](https://leetcode-cn.com/problems/range-sum-query-2d-immutable/solution/xia-ci-ru-he-zai-30-miao-nei-zuo-chu-lai-ptlo/) | 中等 | 🤩🤩🤩🤩🤩 | | [303. 区域和检索 - 数组不可变](https://leetcode-cn.com/problems/range-sum-query-immutable/) | [LeetCode 题解链接](https://leetcode-cn.com/problems/range-sum-query-immutable/solution/sha-shi-qian-zhui-he-ya-tu-jie-qian-zhui-0rla/) | 简单 | 🤩🤩🤩🤩🤩 | | [327. 区间和的个数](https://leetcode.cn/problems/count-of-range-sum/) | [LeetCode 题解链接](https://leetcode.cn/problems/count-of-range-sum/solution/by-ac_oier-b36o/) | 困难 | 🤩🤩🤩 | diff --git "a/LeetCode/201-210/209. \351\225\277\345\272\246\346\234\200\345\260\217\347\232\204\345\255\220\346\225\260\347\273\204\357\274\210\344\270\255\347\255\211\357\274\211.md" "b/LeetCode/201-210/209. \351\225\277\345\272\246\346\234\200\345\260\217\347\232\204\345\255\220\346\225\260\347\273\204\357\274\210\344\270\255\347\255\211\357\274\211.md" new file mode 100644 index 00000000..7e812237 --- /dev/null +++ "b/LeetCode/201-210/209. \351\225\277\345\272\246\346\234\200\345\260\217\347\232\204\345\255\220\346\225\260\347\273\204\357\274\210\344\270\255\347\255\211\357\274\211.md" @@ -0,0 +1,84 @@ +### 题目描述 + +这是 LeetCode 上的 **[209. 长度最小的子数组](https://leetcode-cn.com/problems/implement-trie-prefix-tree/solution/gong-shui-san-xie-yi-ti-shuang-jie-er-we-esm9/)** ,难度为 **中等**。 + +Tag : 「前缀和」、「二分」 + + + +给定一个含有 `n` 个正整数的数组和一个正整数 `target`。 + +找出该数组中满足其和 `≥ target` 的长度最小的 连续子数组 $[nums_l, nums_{l+1}, ..., nums_{r-1}, nums_r]$ ,并返回其长度。如果不存在符合条件的子数组,返回 $0$ 。 + +示例 1: +``` +输入:target = 7, nums = [2,3,1,2,4,3] + +输出:2 + +解释:子数组 [4,3] 是该条件下的长度最小的子数组。 +``` +示例 2: +``` +输入:target = 4, nums = [1,4,4] + +输出:1 +``` +示例 3: +``` +输入:target = 11, nums = [1,1,1,1,1,1,1,1] + +输出:0 +``` + +提示: +* $1 <= target <= 10^9$ +* $1 <= nums.length <= 10^5$ +* $1 <= nums[i] <= 10^5$ + +--- + +### 前缀和 + 二分 + +利用 $nums[i]$ 的数据范围为 $[1, 10^5]$,可知前缀和数组满足「单调递增」。 + +我们先预处理出前缀和数组 `sum`(前缀和数组下标默认从 $1$ 开始),对于每个 $nums[i]$ 而言,假设其对应的前缀和值为 $s = sum[i + 1]$,我们将 $nums[i]$ 视为子数组的右端点,问题转换为:在前缀和数组下标 $[0, i]$ 范围内找到满足 **值小于等于 $s - t$** 的最大下标,充当子数组左端点的前一个值。 + +利用前缀和数组的「单调递增」(即具有二段性),该操作可使用「二分」来做。 + +代码: +```Java +class Solution { + public int minSubArrayLen(int t, int[] nums) { + int n = nums.length, ans = n + 10; + int[] sum = new int[n + 10]; + for (int i = 1; i <= n; i++) sum[i] = sum[i - 1] + nums[i - 1]; + for (int i = 1; i <= n; i++) { + int s = sum[i], d = s - t; + int l = 0, r = i; + while (l < r) { + int mid = l + r + 1 >> 1; + if (sum[mid] <= d) l = mid; + else r = mid - 1; + } + if (sum[r] <= d) ans = Math.min(ans, i - r); + } + return ans == n + 10 ? 0 : ans; + } +} +``` +* 时间复杂度:预处理前缀和数组的复杂度为 $O(n)$,遍历前缀和数组统计答案复杂度为 $O(n\log{n})$。整体复杂度为 $O(n\log{n})$ +* 空间复杂度:$O(n)$ + +--- + +### 最后 + +这是我们「刷穿 LeetCode」系列文章的第 `No.209` 篇,系列开始于 2021/01/01,截止于起始日 LeetCode 上共有 1916 道题目,部分是有锁题,我们将先把所有不带锁的题目刷完。 + +在这个系列文章里面,除了讲解解题思路以外,还会尽可能给出最为简洁的代码。如果涉及通解还会相应的代码模板。 + +为了方便各位同学能够电脑上进行调试和提交代码,我建立了相关的仓库:https://github.com/SharingSource/LogicStack-LeetCode 。 + +在仓库地址里,你可以看到系列文章的题解链接、系列文章的相应代码、LeetCode 原题链接和其他优选题解。 + diff --git "a/LeetCode/551-560/560. \345\222\214\344\270\272 K \347\232\204\345\255\220\346\225\260\347\273\204\357\274\210\344\270\255\347\255\211\357\274\211.md" "b/LeetCode/551-560/560. \345\222\214\344\270\272 K \347\232\204\345\255\220\346\225\260\347\273\204\357\274\210\344\270\255\347\255\211\357\274\211.md" index 40115f3c..b6ffb830 100644 --- "a/LeetCode/551-560/560. \345\222\214\344\270\272 K \347\232\204\345\255\220\346\225\260\347\273\204\357\274\210\344\270\255\347\255\211\357\274\211.md" +++ "b/LeetCode/551-560/560. \345\222\214\344\270\272 K \347\232\204\345\255\220\346\225\260\347\273\204\357\274\210\344\270\255\347\255\211\357\274\211.md" @@ -22,7 +22,7 @@ Tag : 「前缀和」、「哈希表」 ``` 提示: -* $1 <= nums.length <= 2 * 10^4$ +* $1 <= nums.length <= 2 \times 10^4$ * $-1000 <= nums[i] <= 1000$ * $-10^7 <= k <= 10^7$