Skip to content

Instantly share code, notes, and snippets.

@Ifihan
Created December 15, 2025 21:57
Show Gist options
  • Select an option

  • Save Ifihan/32cae8e9db3fde0057aec304bc635410 to your computer and use it in GitHub Desktop.

Select an option

Save Ifihan/32cae8e9db3fde0057aec304bc635410 to your computer and use it in GitHub Desktop.
Number of Smooth Descent Periods of a Stock

Question

Approach

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.

Implementation

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

Complexities

  • Time: O(n)
  • Space: O(1)
image
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment