Search for a command to run...
Try your own input
Change the array and replay the algorithm from step one.
Initialization
For each day, walk backwards while prices stay at or below today's price, and stop at the first strictly higher day.
Brute Force
1function stockSpan(arr, n) {2 const ans = new Array(n);//n: 7ans: [·, ·, ·, ·, ·, ·, ·]3 for (let i = 0; i < n; i++) {4 let currSpan = 0;5 for (let j = i; j >= 0; j--) {6 if (arr[j] <= arr[i]) {7 currSpan++;8 } else break;9 }10 ans[i] = currSpan;11 }12 return ans;13}