Sponsored
Sponsored
Use these hints if you're stuck. Try solving on your own first.
Any array is a series of adjacent longest possible smooth descent periods. For example, [5,3,2,1,7,6] is [5] + [3,2,1] + [7,6].
Think of a 2-pointer approach to traverse the array and find each longest possible period.
Suppose you found the longest possible period with a length of k. How many periods are within that period? How can you count them quickly? Think of the formula to calculate the sum of 1, 2, 3, ..., k.