Created
February 25, 2021 01:29
-
-
Save mpelzsherman/ba1755649406acc5986b9089a8f73bc4 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
function countSubarrays(arr) { | |
var result = []; | |
for (var index = 0; index < arr.length; index++) { | |
var count = 1; // always include the index itself | |
// look forward | |
var subIndex = index + 1; | |
while(arr[subIndex] < arr[index] && subIndex < arr.length) { | |
count++; | |
subIndex++; | |
} | |
// look backward | |
var subIndex = index - 1; | |
while(arr[subIndex] < arr[index] && subIndex >= 0) { | |
count++; | |
subIndex--; | |
} | |
result[index] = count; | |
} | |
return result; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment