Skip to content

Commit 84728c6

Browse files
authored
feat: add weekly contest 425 (#3809)
1 parent b2f46cb commit 84728c6

File tree

20 files changed

+1218
-2
lines changed

20 files changed

+1218
-2
lines changed

solution/0600-0699/0632.Smallest Range Covering Elements from K Lists/README.md

+1-1
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,7 @@ tags:
3232
<pre>
3333
<strong>输入:</strong>nums = [[4,10,15,24,26], [0,9,12,20], [5,18,22,30]]
3434
<strong>输出:</strong>[20,24]
35-
<strong>解释:</strong>
35+
<strong>解释:</strong>
3636
列表 1:[4, 10, 15, 24, 26],24 在区间 [20,24] 中。
3737
列表 2:[0, 9, 12, 20],20 在区间 [20,24] 中。
3838
列表 3:[5, 18, 22, 30],22 在区间 [20,24] 中。
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,206 @@
1+
---
2+
comments: true
3+
difficulty: 简单
4+
edit_url: https://github.com/doocs/leetcode/edit/main/solution/3300-3399/3364.Minimum%20Positive%20Sum%20Subarray/README.md
5+
---
6+
7+
<!-- problem:start -->
8+
9+
# [3364. 最小正和子数组](https://leetcode.cn/problems/minimum-positive-sum-subarray)
10+
11+
[English Version](/solution/3300-3399/3364.Minimum%20Positive%20Sum%20Subarray/README_EN.md)
12+
13+
## 题目描述
14+
15+
<!-- description:start -->
16+
17+
<p>给你一个整数数组 <code>nums</code> 和 <strong>两个</strong> 整数 <code>l</code> 和 <code>r</code>。你的任务是找到一个长度在 <code>l</code> 和 <code>r</code> 之间(包含)且和大于 0 的 <strong>子数组</strong> 的 <strong>最小</strong> 和。</p>
18+
19+
<p>返回满足条件的子数组的 <strong>最小</strong> 和。如果不存在这样的子数组,则返回 -1。</p>
20+
21+
<p><strong>子数组</strong> 是数组中的一个连续 <b>非空</b> 元素序列。</p>
22+
23+
<p>&nbsp;</p>
24+
25+
<p><strong class="example">示例 1:</strong></p>
26+
27+
<div class="example-block">
28+
<p><strong>输入:</strong> <span class="example-io">nums = [3, -2, 1, 4], l = 2, r = 3</span></p>
29+
30+
<p><strong>输出:</strong> <span class="example-io">1</span></p>
31+
32+
<p><strong>解释:</strong></p>
33+
34+
<p>长度在 <code>l = 2</code> 和 <code>r = 3</code> 之间且和大于 0 的子数组有:</p>
35+
36+
<ul>
37+
<li><code>[3, -2]</code> 和为 1</li>
38+
<li><code>[1, 4]</code> 和为 5</li>
39+
<li><code>[3, -2, 1]</code> 和为 2</li>
40+
<li><code>[-2, 1, 4]</code> 和为 3</li>
41+
</ul>
42+
43+
<p>其中,子数组 <code>[3, -2]</code> 的和为 1,是所有正和中最小的。因此,答案为 1。</p>
44+
</div>
45+
46+
<p><strong class="example">示例 2:</strong></p>
47+
48+
<div class="example-block">
49+
<p><strong>输入:</strong> <span class="example-io">nums = [-2, 2, -3, 1], l = 2, r = 3</span></p>
50+
51+
<p><strong>输出:</strong> <span class="example-io">-1</span></p>
52+
53+
<p><strong>解释:</strong></p>
54+
55+
<p>不存在长度在 <code>l</code> 和 <code>r</code> 之间且和大于 0 的子数组。因此,答案为 -1。</p>
56+
</div>
57+
58+
<p><strong class="example">示例 3:</strong></p>
59+
60+
<div class="example-block">
61+
<p><strong>输入:</strong> <span class="example-io">nums = [1, 2, 3, 4], l = 2, r = 4</span></p>
62+
63+
<p><strong>输出:</strong> <span class="example-io">3</span></p>
64+
65+
<p><strong>解释:</strong></p>
66+
67+
<p>子数组 <code>[1, 2]</code> 的长度为 2,和为&nbsp;3,是所有正和中最小的。因此,答案为 3。</p>
68+
</div>
69+
70+
<p>&nbsp;</p>
71+
72+
<p><strong>提示:</strong></p>
73+
74+
<ul>
75+
<li><code>1 &lt;= nums.length &lt;= 100</code></li>
76+
<li><code>1 &lt;= l &lt;= r &lt;= nums.length</code></li>
77+
<li><code>-1000 &lt;= nums[i] &lt;= 1000</code></li>
78+
</ul>
79+
80+
<!-- description:end -->
81+
82+
## 解法
83+
84+
<!-- solution:start -->
85+
86+
### 方法一:枚举
87+
88+
我们可以枚举子数组的左端点 $i$,然后在 $[i, n)$ 的区间内从左往右枚举右端点 $j$,计算区间 $[i, j]$ 的和 $s$,如果 $s$ 大于 0 且区间长度在 $[l, r]$ 之间,我们就更新答案。
89+
90+
最后,如果答案仍然是初始值,说明没有找到符合条件的子数组,返回 $-1$,否则返回答案。
91+
92+
时间复杂度 $O(n^2)$,其中 $n$ 是数组 $\textit{nums}$ 的长度。空间复杂度 $O(1)$。
93+
94+
<!-- tabs:start -->
95+
96+
#### Python3
97+
98+
```python
99+
class Solution:
100+
def minimumSumSubarray(self, nums: List[int], l: int, r: int) -> int:
101+
n = len(nums)
102+
ans = inf
103+
for i in range(n):
104+
s = 0
105+
for j in range(i, n):
106+
s += nums[j]
107+
if l <= j - i + 1 <= r and s > 0:
108+
ans = min(ans, s)
109+
return -1 if ans == inf else ans
110+
```
111+
112+
#### Java
113+
114+
```java
115+
class Solution {
116+
public int minimumSumSubarray(List<Integer> nums, int l, int r) {
117+
int n = nums.size();
118+
final int inf = Integer.MAX_VALUE;
119+
int ans = inf;
120+
for (int i = 0; i < n; ++i) {
121+
int s = 0;
122+
for (int j = i; j < n; ++j) {
123+
s += nums.get(j);
124+
int k = j - i + 1;
125+
if (k >= l && k <= r && s > 0) {
126+
ans = Math.min(ans, s);
127+
}
128+
}
129+
}
130+
return ans == inf ? -1 : ans;
131+
}
132+
}
133+
```
134+
135+
#### C++
136+
137+
```cpp
138+
class Solution {
139+
public:
140+
int minimumSumSubarray(vector<int>& nums, int l, int r) {
141+
int n = nums.size();
142+
const int inf = INT_MAX;
143+
int ans = inf;
144+
for (int i = 0; i < n; ++i) {
145+
int s = 0;
146+
for (int j = i; j < n; ++j) {
147+
s += nums[j];
148+
int k = j - i + 1;
149+
if (k >= l && k <= r && s > 0) {
150+
ans = min(ans, s);
151+
}
152+
}
153+
}
154+
return ans == inf ? -1 : ans;
155+
}
156+
};
157+
```
158+
159+
#### Go
160+
161+
```go
162+
func minimumSumSubarray(nums []int, l int, r int) int {
163+
const inf int = 1 << 30
164+
ans := inf
165+
for i := range nums {
166+
s := 0
167+
for j := i; j < len(nums); j++ {
168+
s += nums[j]
169+
k := j - i + 1
170+
if k >= l && k <= r && s > 0 {
171+
ans = min(ans, s)
172+
}
173+
}
174+
}
175+
if ans == inf {
176+
return -1
177+
}
178+
return ans
179+
}
180+
```
181+
182+
#### TypeScript
183+
184+
```ts
185+
function minimumSumSubarray(nums: number[], l: number, r: number): number {
186+
const n = nums.length;
187+
let ans = Infinity;
188+
for (let i = 0; i < n; ++i) {
189+
let s = 0;
190+
for (let j = i; j < n; ++j) {
191+
s += nums[j];
192+
const k = j - i + 1;
193+
if (k >= l && k <= r && s > 0) {
194+
ans = Math.min(ans, s);
195+
}
196+
}
197+
}
198+
return ans == Infinity ? -1 : ans;
199+
}
200+
```
201+
202+
<!-- tabs:end -->
203+
204+
<!-- solution:end -->
205+
206+
<!-- problem:end -->

0 commit comments

Comments
 (0)