function smallestSubarray (s, arr) { let ws = 0, minLength = arr.length, //Infinity sum = 0, currLength = 0 // Not Required for (let we = 0; we < arr.length; we++) { sum += arr[we]; if (sum > s) { //find length // arr.slice(ws, we).length == we-ws+1 ; Should use RHS instead //missed while loop //while (sum >= s){} Keeps window shrinking, put next 3 lines in while loop minLength = min(arr.slice(ws, we).length, minLength); sum -= arr[ws] ws++ } } }
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