We will maintain a window that grows until sum reach the given sum. Once the window grows to sum at least s then we can start shirking the window from left with the hope to find a smaller window. We shrink until sum falls below s. Then we can grow the window on right again and so on. We keep this procedure of growing-shrinking until the window start reaches the end of the array. Below is the implementation of the above idea which runs in O(n) time and O(1) space.

```
public class Solution {
public int minSubArrayLen(int sum, int[] nums) {
int minlen = Integer.MAX_VALUE;
int curSum = 0;
int start = 0;
int end = 0;
while(start < nums.length){
//if current window doesn't add up to the given sum then
//strech the window to right
if(curSum < sum && end < nums.length){
curSum += nums[end];
end++;
}
//if current window adds up to at least given sum then
//we can shrink the window
else if(curSum >= sum){
minlen = Math.min(minlen, end-start);
curSum -= nums[start];
start++;
}
//cur sum less than required sum but we reach the end
else{
break;
}
}
return (minlen == Integer.MAX_VALUE) ? 0 : minlen;
}
}
```