class Solution { public: int minSubArrayLen(int target, vector<int>& nums) { int n=nums.size(); int l=0,r=0; int sum=0, ans=INT_MAX; while(r < n) { sum+=nums[r]; while(sum >= target) { //if(sum==target) ans = min(ans, r-l+1); sum -= nums[l]; l++; } r++; } if (ans == INT_MAX) return 0; return ans; } };
Preview:
downloadDownload PNG
downloadDownload JPEG
downloadDownload SVG
Tip: You can change the style, width & colours of the snippet with the inspect tool before clicking Download!
Click to optimize width for Twitter