Skip to content

feat: update solutions to lc problems: No.0209,0266 #3372

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Aug 6, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
85 changes: 42 additions & 43 deletions solution/0200-0299/0209.Minimum Size Subarray Sum/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -266,13 +266,15 @@ public class Solution {

### 方法二:双指针

我们可以使用双指针 $j$ 和 $i$ 维护一个窗口,其中窗口中的所有元素之和小于 $target$。初始时 $j = 0$,答案 $ans = n + 1$,其中 $n$ 为数组 $nums$ 的长度
我们注意到,数组 $\textit{nums}$ 中的元素均为正整数,我们可以考虑使用双指针来维护一个滑动窗口

接下来,指针 $i$ 从 $0$ 开始向右移动,每次移动一步,我们将指针 $i$ 对应的元素加入窗口,同时更新窗口中元素之和。如果窗口中元素之和大于等于 $target$,说明当前子数组满足条件,我们可以更新答案,即 $ans = \min(ans, i - j + 1)$。然后我们不断地从窗口中移除元素 $nums[j]$,直到窗口中元素之和小于 $target$,然后重复上述过程
具体地,我们定义两个指针 $\textit{l}$ 和 $\textit{r}$ 分别表示滑动窗口的左边界和右边界,用一个变量 $\textit{s}$ 代表滑动窗口中的元素和

最后,如果 $ans \leq n$,则说明存在满足条件的子数组,返回 $ans$,否则返回 $0$。
在每一步操作中,我们移动右指针 $\textit{r}$,使得滑动窗口中加入一个元素,如果此时 $\textit{s} \ge \textit{target}$,我们就更新最小长度 $\textit{ans} = \min(\textit{ans}, \textit{r} - \textit{l} + 1$,并将左指针 $\textit{l}$ 循环向右移动,直至有 $\textit{s} < \textit{target}$。

最后,如果最小长度 $\textit{ans}$ 仍为初始值,我们就返回 $0$,否则返回 $\textit{ans}$。

时间复杂度 $O(n)$,空间复杂度 $O(1)$。其中 $n$ 为数组 $nums$ 的长度。
时间复杂度 $O(n)$,其中 $n$ 为数组 $\textit{nums}$ 的长度。空间复杂度 $O(1)$

<!-- tabs:start -->

Expand All @@ -281,34 +283,33 @@ public class Solution {
```python
class Solution:
def minSubArrayLen(self, target: int, nums: List[int]) -> int:
n = len(nums)
ans = n + 1
s = j = 0
for i, x in enumerate(nums):
l = s = 0
ans = inf
for r, x in enumerate(nums):
s += x
while j < n and s >= target:
ans = min(ans, i - j + 1)
s -= nums[j]
j += 1
return ans if ans <= n else 0
while s >= target:
ans = min(ans, r - l + 1)
s -= nums[l]
l += 1
return 0 if ans == inf else ans
```

#### Java

```java
class Solution {
public int minSubArrayLen(int target, int[] nums) {
int n = nums.length;
int l = 0, n = nums.length;
long s = 0;
int ans = n + 1;
for (int i = 0, j = 0; i < n; ++i) {
s += nums[i];
while (j < n && s >= target) {
ans = Math.min(ans, i - j + 1);
s -= nums[j++];
for (int r = 0; r < n; ++r) {
s += nums[r];
while (s >= target) {
ans = Math.min(ans, r - l + 1);
s -= nums[l++];
}
}
return ans <= n ? ans : 0;
return ans > n ? 0 : ans;
}
}
```
Expand All @@ -319,17 +320,17 @@ class Solution {
class Solution {
public:
int minSubArrayLen(int target, vector<int>& nums) {
int n = nums.size();
int l = 0, n = nums.size();
long long s = 0;
int ans = n + 1;
for (int i = 0, j = 0; i < n; ++i) {
s += nums[i];
while (j < n && s >= target) {
ans = min(ans, i - j + 1);
s -= nums[j++];
for (int r = 0; r < n; ++r) {
s += nums[r];
while (s >= target) {
ans = min(ans, r - l + 1);
s -= nums[l++];
}
}
return ans == n + 1 ? 0 : ans;
return ans > n ? 0 : ans;
}
};
```
Expand All @@ -338,18 +339,17 @@ public:

```go
func minSubArrayLen(target int, nums []int) int {
n := len(nums)
s := 0
ans := n + 1
for i, j := 0, 0; i < n; i++ {
s += nums[i]
l, n := 0, len(nums)
s, ans := 0, n+1
for r, x := range nums {
s += x
for s >= target {
ans = min(ans, i-j+1)
s -= nums[j]
j++
ans = min(ans, r-l+1)
s -= nums[l]
l++
}
}
if ans == n+1 {
if ans > n {
return 0
}
return ans
Expand All @@ -361,16 +361,15 @@ func minSubArrayLen(target int, nums []int) int {
```ts
function minSubArrayLen(target: number, nums: number[]): number {
const n = nums.length;
let s = 0;
let ans = n + 1;
for (let i = 0, j = 0; i < n; ++i) {
s += nums[i];
let [s, ans] = [0, n + 1];
for (let l = 0, r = 0; r < n; ++r) {
s += nums[r];
while (s >= target) {
ans = Math.min(ans, i - j + 1);
s -= nums[j++];
ans = Math.min(ans, r - l + 1);
s -= nums[l++];
}
}
return ans === n + 1 ? 0 : ans;
return ans > n ? 0 : ans;
}
```

Expand Down
75 changes: 36 additions & 39 deletions solution/0200-0299/0209.Minimum Size Subarray Sum/README_EN.md
Original file line number Diff line number Diff line change
Expand Up @@ -272,34 +272,33 @@ The time complexity is $O(n)$, and the space complexity is $O(1)$. Here, $n$ is
```python
class Solution:
def minSubArrayLen(self, target: int, nums: List[int]) -> int:
n = len(nums)
ans = n + 1
s = j = 0
for i, x in enumerate(nums):
l = s = 0
ans = inf
for r, x in enumerate(nums):
s += x
while j < n and s >= target:
ans = min(ans, i - j + 1)
s -= nums[j]
j += 1
return ans if ans <= n else 0
while s >= target:
ans = min(ans, r - l + 1)
s -= nums[l]
l += 1
return 0 if ans == inf else ans
```

#### Java

```java
class Solution {
public int minSubArrayLen(int target, int[] nums) {
int n = nums.length;
int l = 0, n = nums.length;
long s = 0;
int ans = n + 1;
for (int i = 0, j = 0; i < n; ++i) {
s += nums[i];
while (j < n && s >= target) {
ans = Math.min(ans, i - j + 1);
s -= nums[j++];
for (int r = 0; r < n; ++r) {
s += nums[r];
while (s >= target) {
ans = Math.min(ans, r - l + 1);
s -= nums[l++];
}
}
return ans <= n ? ans : 0;
return ans > n ? 0 : ans;
}
}
```
Expand All @@ -310,17 +309,17 @@ class Solution {
class Solution {
public:
int minSubArrayLen(int target, vector<int>& nums) {
int n = nums.size();
int l = 0, n = nums.size();
long long s = 0;
int ans = n + 1;
for (int i = 0, j = 0; i < n; ++i) {
s += nums[i];
while (j < n && s >= target) {
ans = min(ans, i - j + 1);
s -= nums[j++];
for (int r = 0; r < n; ++r) {
s += nums[r];
while (s >= target) {
ans = min(ans, r - l + 1);
s -= nums[l++];
}
}
return ans == n + 1 ? 0 : ans;
return ans > n ? 0 : ans;
}
};
```
Expand All @@ -329,18 +328,17 @@ public:

```go
func minSubArrayLen(target int, nums []int) int {
n := len(nums)
s := 0
ans := n + 1
for i, j := 0, 0; i < n; i++ {
s += nums[i]
l, n := 0, len(nums)
s, ans := 0, n+1
for r, x := range nums {
s += x
for s >= target {
ans = min(ans, i-j+1)
s -= nums[j]
j++
ans = min(ans, r-l+1)
s -= nums[l]
l++
}
}
if ans == n+1 {
if ans > n {
return 0
}
return ans
Expand All @@ -352,16 +350,15 @@ func minSubArrayLen(target int, nums []int) int {
```ts
function minSubArrayLen(target: number, nums: number[]): number {
const n = nums.length;
let s = 0;
let ans = n + 1;
for (let i = 0, j = 0; i < n; ++i) {
s += nums[i];
let [s, ans] = [0, n + 1];
for (let l = 0, r = 0; r < n; ++r) {
s += nums[r];
while (s >= target) {
ans = Math.min(ans, i - j + 1);
s -= nums[j++];
ans = Math.min(ans, r - l + 1);
s -= nums[l++];
}
}
return ans === n + 1 ? 0 : ans;
return ans > n ? 0 : ans;
}
```

Expand Down
16 changes: 8 additions & 8 deletions solution/0200-0299/0209.Minimum Size Subarray Sum/Solution2.cpp
Original file line number Diff line number Diff line change
@@ -1,16 +1,16 @@
class Solution {
public:
int minSubArrayLen(int target, vector<int>& nums) {
int n = nums.size();
int l = 0, n = nums.size();
long long s = 0;
int ans = n + 1;
for (int i = 0, j = 0; i < n; ++i) {
s += nums[i];
while (j < n && s >= target) {
ans = min(ans, i - j + 1);
s -= nums[j++];
for (int r = 0; r < n; ++r) {
s += nums[r];
while (s >= target) {
ans = min(ans, r - l + 1);
s -= nums[l++];
}
}
return ans == n + 1 ? 0 : ans;
return ans > n ? 0 : ans;
}
};
};
19 changes: 9 additions & 10 deletions solution/0200-0299/0209.Minimum Size Subarray Sum/Solution2.go
Original file line number Diff line number Diff line change
@@ -1,17 +1,16 @@
func minSubArrayLen(target int, nums []int) int {
n := len(nums)
s := 0
ans := n + 1
for i, j := 0, 0; i < n; i++ {
s += nums[i]
l, n := 0, len(nums)
s, ans := 0, n+1
for r, x := range nums {
s += x
for s >= target {
ans = min(ans, i-j+1)
s -= nums[j]
j++
ans = min(ans, r-l+1)
s -= nums[l]
l++
}
}
if ans == n+1 {
if ans > n {
return 0
}
return ans
}
}
16 changes: 8 additions & 8 deletions solution/0200-0299/0209.Minimum Size Subarray Sum/Solution2.java
Original file line number Diff line number Diff line change
@@ -1,15 +1,15 @@
class Solution {
public int minSubArrayLen(int target, int[] nums) {
int n = nums.length;
int l = 0, n = nums.length;
long s = 0;
int ans = n + 1;
for (int i = 0, j = 0; i < n; ++i) {
s += nums[i];
while (j < n && s >= target) {
ans = Math.min(ans, i - j + 1);
s -= nums[j++];
for (int r = 0; r < n; ++r) {
s += nums[r];
while (s >= target) {
ans = Math.min(ans, r - l + 1);
s -= nums[l++];
}
}
return ans <= n ? ans : 0;
return ans > n ? 0 : ans;
}
}
}
17 changes: 8 additions & 9 deletions solution/0200-0299/0209.Minimum Size Subarray Sum/Solution2.py
Original file line number Diff line number Diff line change
@@ -1,12 +1,11 @@
class Solution:
def minSubArrayLen(self, target: int, nums: List[int]) -> int:
n = len(nums)
ans = n + 1
s = j = 0
for i, x in enumerate(nums):
l = s = 0
ans = inf
for r, x in enumerate(nums):
s += x
while j < n and s >= target:
ans = min(ans, i - j + 1)
s -= nums[j]
j += 1
return ans if ans <= n else 0
while s >= target:
ans = min(ans, r - l + 1)
s -= nums[l]
l += 1
return 0 if ans == inf else ans
13 changes: 6 additions & 7 deletions solution/0200-0299/0209.Minimum Size Subarray Sum/Solution2.ts
Original file line number Diff line number Diff line change
@@ -1,13 +1,12 @@
function minSubArrayLen(target: number, nums: number[]): number {
const n = nums.length;
let s = 0;
let ans = n + 1;
for (let i = 0, j = 0; i < n; ++i) {
s += nums[i];
let [s, ans] = [0, n + 1];
for (let l = 0, r = 0; r < n; ++r) {
s += nums[r];
while (s >= target) {
ans = Math.min(ans, i - j + 1);
s -= nums[j++];
ans = Math.min(ans, r - l + 1);
s -= nums[l++];
}
}
return ans === n + 1 ? 0 : ans;
return ans > n ? 0 : ans;
}
Loading
Loading