Sponsored
Sponsored
Use these hints if you're stuck. Try solving on your own first.
For each element <code>nums[i]</code>, we can count the number of valid subarrays ending with it.
For each index <code>i</code>, find the nearest index <code>j</code> on its left <code>(j < i)</code> such that <code>nums[j] < nums[i]</code>. This can be done via a monotonic stack.
For each index <code>i</code>, find the number of indices <code>k</code> in the window <code>[j + 1, i]</code> such that <code>nums[k] == nums[i]</code>, this is the number of the valid subarrays ending with <code>nums[i]</code>. This can be done by sliding window.
Sum the answer of all the indices <code>i</code> to get the final result.
Is it possible to use DSU as an alternate solution?