Search for a command to run...
Try your own input
Change the array and replay the algorithm from step one.
Initialization
There are n(n+1)/2 subarrays. For each start i we extend the end j rightwards, keeping a running minimum so each extension costs O(1).
Brute Force
1function sumSubarrayMins(arr) {2 const n = arr.length;3 const mod = 1e9 + 7;4 let sum = 0;//n: 4sum: 05 for (let i = 0; i < n; i++) {6 let mini = arr[i];7 for (let j = i; j < n; j++) {8 mini = Math.min(mini, arr[j]);9 sum = (sum + mini) % mod;10 }11 }12 return sum;13}