Leetcode Problem 2110. Number of Smooth Descent Periods of a Stock

2110. Number of Smooth Descent Periods of a Stock

Leetcode Solutions

Sliding Window Approach

  1. Initialize a counter count to 1 (since a single day is a smooth descent period by definition).
  2. Initialize a variable total_periods to 0 to keep track of the total number of smooth descent periods.
  3. Iterate through the prices array starting from the second element.
  4. For each element, check if it is exactly 1 less than the previous element.
    • If it is, increment count (expand the window).
    • If it is not, add the current count to total_periods and reset count to 1.
  5. After the loop, add the last count to total_periods to include the final window.
  6. Return total_periods as the result.
UML Thumbnail

Dynamic Programming Approach

Ask Question

Programming Language
image/screenshot of info(optional)
Full Screen
Loading...

Suggested Answer

Answer
Full Screen
Copy Answer Code
Loading...