I scan the array once while tracking the length of the current smooth descent streak. If prices[i] == prices[i-1] - 1, the streak continues and I increment its length; otherwise, I reset the streak length to 1. At each index, I add the current streak length to the answer because every prefix of the current streak forms a valid smooth descent period ending at that day. This way, all single-day and multi-day valid periods are counted in one pass.
class Solution:
def getDescentPeriods(self, prices: List[int]) -> int:
ans = 0
curr = 0
for i in range(len(prices)):
if i > 0 and prices[i] == prices[i - 1] - 1:
curr += 1
else:
curr = 1
ans += curr
return ans- Time: O(n)
- Space: O(1)