public int minSubArrayLen(int s, int[] a) {
if (a == null  a.length == 0)
return 0;
int i = 0, j = 0, sum = 0, min = Integer.MAX_VALUE;
while (j < a.length) {
sum += a[j++];
while (sum >= s) {
min = Math.min(min, j  i);
sum = a[i++];
}
}
return min == Integer.MAX_VALUE ? 0 : min;
}
Accepted clean Java O(n) solution (two pointers)


said in Accepted clean Java O(n) solution (two pointers):
Translated to python
import sys class Solution(object): def minSubArrayLen(self, s, nums): """ :type s: int :type nums: List[int] :rtype: int """ if not nums: return 0 right = left = 0 minL = sys.maxsize sumV = 0 while right < len(nums): sumV += nums[right] right+=1 while sumV >= s: minL = min (minL , rightleft) sumV = nums[left] left+=1 if minL == sys.maxsize: return 0 return minL



Perhaps could make it more readable. Thanks for sharing!
public int minSubArrayLen(int s, int[] nums) { int sum = 0, from = 0, win = Integer.MAX_VALUE; for (int i = 0; i < nums.length; i++) { sum += nums[i]; while (sum >= s) { win = Math.min(win, i  from + 1); sum = nums[from++]; } } return (win == Integer.MAX_VALUE) ? 0 : win; }

This solution using two pointers is actually wrong
See https://discuss.leetcode.com/topic/62885/wrongtestcasesGive u an counterexample
input:
7
[1, 3, 4, 1]

@qingemeng Do you ask due to a doubt about the two pointers? There are two pointers but both of them go forward (no backtracking) . In fact, this is O(2n) which also means O(n).
