LeetCode 2875. 无限数组的最短子数组
原题链接
中等
作者:
我是java同学
,
2023-10-02 21:15:38
,
所有人可见
,
阅读 61
class Solution {
public:
typedef long long LL;
int minSizeSubarray(vector<int>& nums, int target) {
LL tot = 0;
for (int x: nums) tot += x;
int res = 1e9;
int n = nums.size();
for (int i = 0, j = 0, sum = 0; i < n * 2; i ++ ) {
sum += nums[i % n];
while (sum > target % tot) {
sum -= nums[j % n];
j ++ ;
}
if (sum == target % tot)
res = min(res, i - j + 1);
}
if (res == 1e9) return -1;
return res + target / tot * n;
}
};