Minimum window size for sum >= target
Java
Hard
4 views
Problem Description
Task: given positive integers, return min length subarray with sum >= target. If not found return 0.
Output Format
Return value
Constraints
Use sliding window.
Official Solution
static int minLenAtLeast(int[] a,int target){int l=0;long s=0;int best=Integer.MAX_VALUE;for(int r=0;r<a.length;r++){s+=a[r];while(s>=target){best=Math.min(best,r-l+1);s-=a[l++];}}return best==Integer.MAX_VALUE?0:best;}
Solutions (0)
No solutions submitted yet. Be the first!
No comments yet. Start the discussion!