Skip to content

Commit ff3da73

Browse files
committed
209-minimum-size-subarray-sum.md Added Chinese translation.
1 parent 61293fc commit ff3da73

File tree

2 files changed

+123
-27
lines changed

2 files changed

+123
-27
lines changed

solutions/1-1000/209-minimum-size-subarray-sum.md

+115-27
Original file line numberDiff line numberDiff line change
@@ -1,47 +1,77 @@
11
# 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+
[中文题解](#中文题解)
36

47
## LeetCode problem description
58
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.
69

710
* A **subarray** is a contiguous non-empty sequence of elements within an array.
811

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**
1513

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]`
2116

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`
2718

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]
2932
- `1 <= target <= 10 ** 9`
3033
- `1 <= nums.length <= 100000`
3134
- `1 <= nums[i] <= 10000`
3235

3336
## 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**.
3538

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.
3841
* `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;
4575
```
4676

4777
## Complexity
@@ -191,3 +221,61 @@ public class Solution
191221
```
192222
// Welcome to create a PR to complete the code of this language, thanks!
193223
```
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+
```

solutions/3001-4000/unorganized.md

+8
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,14 @@ You want to query information by a student's name.
1414
## Binary tree unified stack iteration
1515
`boolean mark` solution.
1616

17+
## Other Algorithms
18+
### Binary Search Algorithm
19+
704
20+
21+
### Fast and Slow Pointers
22+
27
23+
24+
1725
## Dynamic programming
1826
- [647. Palindromic Substrings](https://leetcode.cn/problems/palindromic-substrings/)
1927
- [516. Longest Palindromic Subsequence](https://leetcode.cn/problems/longest-palindromic-subsequence/)

0 commit comments

Comments
 (0)