|
1 | 1 | # 209. Minimum Size Subarray Sum - LeetCode Solution
|
2 |
| -LeetCode problem link: [209. Minimum Size Subarray Sum](https://leetcode.com/problems/minimum-size-subarray-sum) |
| 2 | +LeetCode problem link: [209. Minimum Size Subarray Sum](https://leetcode.com/problems/minimum-size-subarray-sum), |
| 3 | +[209. 长度最小的子数组](https://leetcode.cn/problems/minimum-size-subarray-sum) |
| 4 | + |
| 5 | +[中文题解](#中文题解) |
3 | 6 |
|
4 | 7 | ## LeetCode problem description
|
5 | 8 | Given an array of positive integers `nums` and a positive integer `target`, return _the **minimal length** of a **subarray** whose sum is greater than or equal to `target`_. If there is no such subarray, return `0` instead.
|
6 | 9 |
|
7 | 10 | * A **subarray** is a contiguous non-empty sequence of elements within an array.
|
8 | 11 |
|
9 |
| -### Example 1 |
10 |
| -```ruby |
11 |
| -Input: target = 7, nums = [2,3,1,2,4,3] |
12 |
| -Output: 2 |
13 |
| -Explanation: The subarray [4,3] has the minimal length under the problem constraint. |
14 |
| -``` |
| 12 | +Difficulty: **Medium** |
15 | 13 |
|
16 |
| -### Example 2 |
17 |
| -```ruby |
18 |
| -Input: target = 4, nums = [1,4,4] |
19 |
| -Output: 1 |
20 |
| -``` |
| 14 | +### [Example 1] |
| 15 | +**Input**: `target = 7, nums = [2,3,1,2,4,3]` |
21 | 16 |
|
22 |
| -### Example 3 |
23 |
| -```ruby |
24 |
| -Input: target = 11, nums = [1,1,1,1,1,1,1,1] |
25 |
| -Output: 0 |
26 |
| -``` |
| 17 | +**Output**: `2` |
27 | 18 |
|
28 |
| -### Constraints |
| 19 | +**Explanation**: `The subarray [4,3] has the minimal length under the problem constraint.` |
| 20 | + |
| 21 | +### [Example 2] |
| 22 | +**Input**: `target = 4, nums = [1,4,4]` |
| 23 | + |
| 24 | +**Output**: `1` |
| 25 | + |
| 26 | +### [Example 3] |
| 27 | +**Input**: `target = 11, nums = [1,1,1,1,1,1,1,1]` |
| 28 | + |
| 29 | +**Output**: `0` |
| 30 | + |
| 31 | +### [Constraints] |
29 | 32 | - `1 <= target <= 10 ** 9`
|
30 | 33 | - `1 <= nums.length <= 100000`
|
31 | 34 | - `1 <= nums[i] <= 10000`
|
32 | 35 |
|
33 | 36 | ## Intuition behind the Solution
|
34 |
| -For **subarray** problems, you can consider using **Sliding Window Technique**, which belongs to the **Fast and Slow Pointers Technique**. |
| 37 | +For **subarray** problems, you can consider using **Sliding Window Technique**, which is similar to the **Fast and Slow Pointers Technique**. |
35 | 38 |
|
36 |
| -## Steps to the Solution |
37 |
| -* Traverse the `nums` array, the `index` of the element is named `fast_index`. This is the most important logic of _Fast and Slow Pointers Technique_. You'd better memorize it. |
| 39 | +## Steps |
| 40 | +* Iterate over the `nums` array, the `index` of the element is named `fastIndex`. Although inconspicuous, this is the most important logic of the _Fast and Slow Pointers Technique_. Please memorize it. |
38 | 41 | * `sum += nums[fast_index]`.
|
39 |
| -* If `sum >= target`, `sum -= nums[slow_index]; slow_index += 1`: |
40 |
| -```python |
41 |
| -while sum >= target: |
42 |
| - min_length = min(min_length, fast_index - slow_index + 1) |
43 |
| - sum -= nums[slow_index] |
44 |
| - slow_index += 1 |
| 42 | +```java |
| 43 | +var minLength = Integer.MAX_VALUE; |
| 44 | +var sum = 0; |
| 45 | +var slowIndex = 0; |
| 46 | + |
| 47 | +for (var fastIndex = 0; fastIndex < nums.length; fastIndex++) { // This line the most important logic of the `Fast and Slow Pointers Technique`. |
| 48 | + sum += nums[fastIndex]; // 1 |
| 49 | +} |
| 50 | + |
| 51 | +return minLength; |
| 52 | +``` |
| 53 | + |
| 54 | +* Control of `slowIndex`: |
| 55 | +```java |
| 56 | +var minLength = Integer.MAX_VALUE; |
| 57 | +var sum = 0; |
| 58 | +var slowIndex = 0; |
| 59 | + |
| 60 | +for (var fastIndex = 0; fastIndex < nums.length; fastIndex++) { |
| 61 | + sum += nums[fastIndex]; |
| 62 | + |
| 63 | + while (sum >= target) { // 1 |
| 64 | + minLength = Math.min(minLength, fastIndex - slowIndex + 1); // 2 |
| 65 | + sum -= nums[slowIndex]; // 3 |
| 66 | + slowIndex++; // 4 |
| 67 | + } |
| 68 | +} |
| 69 | + |
| 70 | +if (minLength == Integer.MAX_VALUE) { // 5 |
| 71 | + return 0; // 6 |
| 72 | +} |
| 73 | + |
| 74 | +return minLength; |
45 | 75 | ```
|
46 | 76 |
|
47 | 77 | ## Complexity
|
@@ -191,3 +221,61 @@ public class Solution
|
191 | 221 | ```
|
192 | 222 | // Welcome to create a PR to complete the code of this language, thanks!
|
193 | 223 | ```
|
| 224 | + |
| 225 | +## 力扣问题描述 |
| 226 | +给定一个含有 `n` 个正整数的数组和一个正整数 `target` 。 |
| 227 | + |
| 228 | +找出该数组中满足其总和大于等于 `target` 的长度**最小的** **子数组** `[numsl, numsl+1, ..., numsr-1, numsr]` ,并返回*其长度*。如果不存在符合条件的子数组,返回 `0` 。 |
| 229 | + |
| 230 | +**子数组** 是数组中连续的 **非空** 元素序列。 |
| 231 | + |
| 232 | +难度: **中等** |
| 233 | + |
| 234 | +### [示例 1] |
| 235 | +**输入**: `target = 7, nums = [2,3,1,2,4,3]` |
| 236 | + |
| 237 | +**输出**: `2` |
| 238 | + |
| 239 | +**解释**: `子数组 [4,3] 是该条件下的长度最小的子数组。` |
| 240 | + |
| 241 | +# 中文题解 |
| 242 | +## 思路 |
| 243 | +1. 对于**子数组**问题,可以考虑使用**滑动窗口技术**,它类似于**快速和慢速指针技术**。 |
| 244 | + |
| 245 | +## 步骤 |
| 246 | +1. **遍历** `nums` 数组,元素的 `index` 可命名为 `fastIndex`。虽然不起眼,但这是 `快慢指针技术` **最重要**的逻辑。请最好记住它。 |
| 247 | +2. `sum += nums[fast_index]`. |
| 248 | +```java |
| 249 | +var minLength = Integer.MAX_VALUE; |
| 250 | +var sum = 0; |
| 251 | +var slowIndex = 0; |
| 252 | + |
| 253 | +for (var fastIndex = 0; fastIndex < nums.length; fastIndex++) { // 本行是`快慢指针技术`最重要的逻辑 |
| 254 | + sum += nums[fastIndex]; // 1 |
| 255 | +} |
| 256 | + |
| 257 | +return minLength; |
| 258 | +``` |
| 259 | + |
| 260 | +3. 控制`slowIndex`。 |
| 261 | +```java |
| 262 | +var minLength = Integer.MAX_VALUE; |
| 263 | +var sum = 0; |
| 264 | +var slowIndex = 0; |
| 265 | + |
| 266 | +for (var fastIndex = 0; fastIndex < nums.length; fastIndex++) { |
| 267 | + sum += nums[fastIndex]; |
| 268 | + |
| 269 | + while (sum >= target) { // 1 |
| 270 | + minLength = Math.min(minLength, fastIndex - slowIndex + 1); // 2 |
| 271 | + sum -= nums[slowIndex]; // 3 |
| 272 | + slowIndex++; // 4 |
| 273 | + } |
| 274 | +} |
| 275 | + |
| 276 | +if (minLength == Integer.MAX_VALUE) { // 5 |
| 277 | + return 0; // 6 |
| 278 | +} |
| 279 | + |
| 280 | +return minLength; |
| 281 | +``` |
0 commit comments