3364. Minimum Positive Sum Subarray Easy
1/**
2 * [3364] Minimum Positive Sum Subarray
3 *
4 * You are given an integer array nums and two integers l and r. Your task is to find the minimum sum of a subarray whose size is between l and r (inclusive) and whose sum is greater than 0.
5 * Return the minimum sum of such a subarray. If no such subarray exists, return -1.
6 * A subarray is a contiguous non-empty sequence of elements within an array.
7 *
8 * <strong class="example">Example 1:
9 * <div class="example-block">
10 * Input: <span class="example-io">nums = [3, -2, 1, 4], l = 2, r = 3</span>
11 * Output: <span class="example-io">1</span>
12 * Explanation:
13 * The subarrays of length between l = 2 and r = 3 where the sum is greater than 0 are:
14 *
15 * [3, -2] with a sum of 1
16 * [1, 4] with a sum of 5
17 * [3, -2, 1] with a sum of 2
18 * [-2, 1, 4] with a sum of 3
19 *
20 * Out of these, the subarray [3, -2] has a sum of 1, which is the smallest positive sum. Hence, the answer is 1.
21 * </div>
22 * <strong class="example">Example 2:
23 * <div class="example-block">
24 * Input: <span class="example-io">nums = [-2, 2, -3, 1], l = 2, r = 3</span>
25 * Output: <span class="example-io">-1</span>
26 * Explanation:
27 * There is no subarray of length between l and r that has a sum greater than 0. So, the answer is -1.
28 * </div>
29 * <strong class="example">Example 3:
30 * <div class="example-block">
31 * Input: <span class="example-io">nums = [1, 2, 3, 4], l = 2, r = 4</span>
32 * Output: <span class="example-io">3</span>
33 * Explanation:
34 * The subarray [1, 2] has a length of 2 and the minimum sum greater than 0. So, the answer is 3.
35 * </div>
36 *
37 * Constraints:
38 *
39 * 1 <= nums.length <= 100
40 * 1 <= l <= r <= nums.length
41 * -1000 <= nums[i] <= 1000
42 *
43 */
44pub struct Solution {}
45
46// problem: https://leetcode.com/problems/minimum-positive-sum-subarray/
47// discuss: https://leetcode.com/problems/minimum-positive-sum-subarray/discuss/?currentPage=1&orderBy=most_votes&query=
48
49// submission codes start here
50
51impl Solution {
52 pub fn minimum_sum_subarray(nums: Vec<i32>, l: i32, r: i32) -> i32 {
53 0
54 }
55}
56
57// submission codes end
58
59#[cfg(test)]
60mod tests {
61 use super::*;
62
63 #[test]
64 fn test_3364() {
65 }
66}
67
Back
© 2025 bowen.ge All Rights Reserved.