2875. Minimum Size Subarray in Infinite Array Medium
1/**
2 * [2875] Minimum Size Subarray in Infinite Array
3 *
4 * You are given a 0-indexed array nums and an integer target.
5 * A 0-indexed array infinite_nums is generated by infinitely appending the elements of nums to itself.
6 * Return the length of the shortest subarray of the array infinite_nums with a sum equal to target. If there is no such subarray return -1.
7 *
8 * <strong class="example">Example 1:
9 *
10 * Input: nums = [1,2,3], target = 5
11 * Output: 2
12 * Explanation: In this example infinite_nums = [1,2,3,1,2,3,1,2,...].
13 * The subarray in the range [1,2], has the sum equal to target = 5 and length = 2.
14 * It can be proven that 2 is the shortest length of a subarray with sum equal to target = 5.
15 *
16 * <strong class="example">Example 2:
17 *
18 * Input: nums = [1,1,1,2,3], target = 4
19 * Output: 2
20 * Explanation: In this example infinite_nums = [1,1,1,2,3,1,1,1,2,3,1,1,...].
21 * The subarray in the range [4,5], has the sum equal to target = 4 and length = 2.
22 * It can be proven that 2 is the shortest length of a subarray with sum equal to target = 4.
23 *
24 * <strong class="example">Example 3:
25 *
26 * Input: nums = [2,4,6,8], target = 3
27 * Output: -1
28 * Explanation: In this example infinite_nums = [2,4,6,8,2,4,6,8,...].
29 * It can be proven that there is no subarray with sum equal to target = 3.
30 *
31 *
32 * Constraints:
33 *
34 * 1 <= nums.length <= 10^5
35 * 1 <= nums[i] <= 10^5
36 * 1 <= target <= 10^9
37 *
38 */
39pub struct Solution {}
40
41// problem: https://leetcode.com/problems/minimum-size-subarray-in-infinite-array/
42// discuss: https://leetcode.com/problems/minimum-size-subarray-in-infinite-array/discuss/?currentPage=1&orderBy=most_votes&query=
43
44// submission codes start here
45
46impl Solution {
47 pub fn min_size_subarray(nums: Vec<i32>, target: i32) -> i32 {
48 0
49 }
50}
51
52// submission codes end
53
54#[cfg(test)]
55mod tests {
56 use super::*;
57
58 #[test]
59 fn test_2875() {
60 }
61}
62
Back
© 2025 bowen.ge All Rights Reserved.