Watch 10 video solutions for Number of Smooth Descent Periods of a Stock, a medium level problem involving Array, Math, Dynamic Programming. This walkthrough by codestorywithMIK has 4,579 views views. Want to try solving it yourself? Practice on FleetCode or read the detailed text solution.
You are given an integer array prices representing the daily price history of a stock, where prices[i] is the stock price on the ith day.
A smooth descent period of a stock consists of one or more contiguous days such that the price on each day is lower than the price on the preceding day by exactly 1. The first day of the period is exempted from this rule.
Return the number of smooth descent periods.
Example 1:
Input: prices = [3,2,1,4] Output: 7 Explanation: There are 7 smooth descent periods: [3], [2], [1], [4], [3,2], [2,1], and [3,2,1] Note that a period with one day is a smooth descent period by the definition.
Example 2:
Input: prices = [8,6,7,7] Output: 4 Explanation: There are 4 smooth descent periods: [8], [6], [7], and [7] Note that [8,6] is not a smooth descent period as 8 - 6 ≠ 1.
Example 3:
Input: prices = [1] Output: 1 Explanation: There is 1 smooth descent period: [1]
Constraints:
1 <= prices.length <= 1051 <= prices[i] <= 105Problem Overview: You receive an integer array prices representing the daily stock price. A smooth descent period is a contiguous subarray where each element is exactly 1 less than the previous element. Every single element also counts as a valid period. The goal is to count the total number of such periods across the entire array.
Approach 1: Using a Stack (O(n) time, O(n) space)
This approach tracks the current descent sequence using a stack. Iterate through the array and compare each price with the previous value. If prices[i] == prices[i-1] - 1, the descent continues, so push the value onto the stack and increase the running count by the current stack size. If the condition breaks, reset the stack because the descent sequence ends and a new one begins. The key insight: every extension of a valid descent sequence creates multiple new subarrays ending at the current index. The stack effectively stores the active decreasing run. This approach is intuitive when reasoning about contiguous patterns in array traversal and makes it easy to visualize how subarrays expand.
Approach 2: Recursive Evaluation (O(n) time, O(n) space)
The recursive method evaluates the length of the descent sequence starting at each index. If prices[i+1] == prices[i] - 1, the sequence extends and the recursion continues to the next index. Otherwise, the recursion terminates and returns 1 because a single element still forms a valid descent period. Memoization stores previously computed results to avoid recomputation, turning the recursion into a topβdown dynamic programming solution. Each recursive call calculates how many valid subarrays start at that position, and the final answer sums all results. The logic closely mirrors the mathematical definition of the descent rule and works well when modeling state transitions.
Both methods rely on the same observation: if a descending run has length k, it contributes k * (k + 1) / 2 valid subarrays. Recognizing this pattern simplifies counting and connects the solution to basic math reasoning about sequences.
Recommended for interviews: The linear scan approach implemented with stack-like tracking is what most interviewers expect. It processes the array once, maintains a running descent length, and accumulates counts in O(n) time. The recursive formulation shows deeper understanding of the state transition and can help explain the dynamic programming structure, but iterative O(n) traversal demonstrates stronger practical optimization skills.
| Approach | Time | Space | When to Use |
|---|---|---|---|
| Stack-based Descent Tracking | O(n) | O(n) | Best general solution for scanning arrays and counting valid descent segments efficiently |
| Recursive Evaluation with Memoization | O(n) | O(n) | Useful for demonstrating dynamic programming reasoning or recursive state transitions |