There are lots of explanations for the following O(N) approach and I think the code is self-explanatory:

```
public class Solution {
public int minSubArrayLen(int s, int[] nums) {
int lo=0, hi=0, sum=0, minLen=nums.length;
boolean hasMin=false;
while(hi<nums.length){
while(sum<s&&hi<nums.length)
sum+=nums[hi++];
while(sum-nums[lo]>=s)
sum-=nums[lo++];
if(!hasMin&&sum>=s) hasMin=true;
minLen=Math.min(minLen, hi-lo);
sum-=nums[lo++];
}
return hasMin? minLen:0;
}
}
```

For the O(NlogN) approach, we have to use divide-and-conquer. Divide the array into 2 halves at the middle and exluding the middle element, the left subarray will have indices from `0`

to `middle-1`

, and the right subarray will have indices from `middle+1`

to `nums.length-1`

.

Now the mininum length can come from 3 places: 1) the left subarray 2) the right subarray and 3) the subarray which contains the `middle`

element. For case 1) and 2), we can call recursively to get the min lengths. However, for 3), we need to apply the O(N) approach above (so the O(NlogN) approach is slower, uses more space and harder to code......) and make sure that we always include the `middle`

element. The complexity is T(N)=2T(N/2)+O(N) so T(N)=O(NlogN). My O(NlogN) approach will lead to TLE for the largest test case, but I tested it myself using some small test cases. Let me know if you find any errors and thanks in advance.

```
public class Solution {
public int minSubArrayLen(int s, int[] nums) {
int minLen=minSubArrayLen(s, nums, 0, nums.length-1);
return minLen==Integer.MAX_VALUE? 0:minLen;
}
private int minSubArrayLen(int s, int[] nums, int lo, int hi){
if(hi<lo||hi==lo&&nums[hi]<s) return Integer.MAX_VALUE;
if(hi==lo&&nums[hi]>=s) return 1;
int mid=(lo+hi)/2;
int left=minSubArrayLen(s, nums, lo, mid-1), right=minSubArrayLen(s, nums, mid+1, hi);
int sum=nums[mid], start=mid, end=mid+1, minLen=Integer.MAX_VALUE;
while(sum<s&&start>0)
sum+=nums[--start];
if(sum>=s) minLen=Math.min(minLen, end-start);
while(start<=mid){
sum-=nums[start++];
while(sum<s&&end<nums.length)
sum+=nums[end++];
if(sum>=s) minLen=Math.min(minLen, end-start);
}
return Math.min(minLen, Math.min(left, right));
}
}
```