-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathSolution.cs
More file actions
28 lines (27 loc) · 841 Bytes
/
Solution.cs
File metadata and controls
28 lines (27 loc) · 841 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
namespace LeetCode.Medium.Problem209{
//209. Minimum Size Subarray Sum
//https://leetcode.com/problems/minimum-size-subarray-sum/
/*
Given an array of positive integers nums and a positive integer target, return the minimal length of a
subarray
whose sum is greater than or equal to target. If there is no such subarray, return 0 instead.
*/
public class Solution {
public int MinSubArrayLen(int target, int[] nums) {
int left = 0;
int ans = int.MaxValue;
int sum = 0;
for (int right = 0; right < nums.Length; right++)
{
sum += nums[right];
while (sum >= target)
{
ans = Math.Min(ans, right - left + 1);
sum -= nums[left];
left++;
}
}
return ans == int.MaxValue ? 0 : ans;
}
}
}