Number of Smooth Descent Periods of a Stock
LeetCode 2233 | Difficulty: Mediumβ
MediumProblem Descriptionβ
You are given an integer array prices representing the daily price history of a stock, where prices[i] is the stock price on the i^th 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 <= 10^5`
- `1 <= prices[i] <= 10^5`
Topics: Array, Math, Two Pointers, Dynamic Programming, Sliding Window
Approachβ
Two Pointersβ
Use two pointers to traverse the array, reducing the search space at each step. This avoids the need for nested loops, bringing complexity from O(nΒ²) to O(n) or O(n log n) if sorting is involved.
Array is sorted or can be sorted, and you need to find pairs/triplets that satisfy a condition.
Sliding Windowβ
Maintain a window [left, right] over the array/string. Expand right to include new elements, and shrink left when the window violates constraints. Track the optimal answer as the window slides.
Finding subarrays/substrings with a property (max length, min length, exact count).
Dynamic Programmingβ
Break the problem into overlapping subproblems. Define a state (what information do you need?), a recurrence (how does state[i] depend on smaller states?), and a base case. Consider both top-down (memoization) and bottom-up (tabulation) approaches.
Optimal substructure + overlapping subproblems (counting ways, min/max cost, feasibility).
Mathematicalβ
Look for mathematical patterns or formulas. Consider: modular arithmetic, GCD/LCM, prime factorization, combinatorics, or geometric properties.
Problems with clear mathematical structure, counting, number properties.
Solutionsβ
Solution 1: C# (Best: 451 ms)β
| Metric | Value |
|---|---|
| Runtime | 451 ms |
| Memory | 45.7 MB |
| Date | 2022-01-17 |
public class Solution {
public long GetDescentPeriods(int[] prices) {
long sum = 1;
long temp = 1;
for (int i = 1; i < prices.Length; i++)
{
if (prices[i] == (prices[i - 1]-1))
{
temp++;
}
else { temp = 1; }
sum += temp;
}
return sum;
}
}
Complexity Analysisβ
| Approach | Time | Space |
|---|---|---|
| Two Pointers | $O(n)$ | $O(1)$ |
| Sliding Window | $O(n)$ | $O(k)$ |
| Dynamic Programming | $O(n)$ | $O(n)$ |
Interview Tipsβ
- Discuss the brute force approach first, then optimize. Explain your thought process.
- Ask: "Can I sort the array?" β Sorting often enables two-pointer techniques.
- Clarify what makes a window "valid" and what triggers expansion vs shrinking.
- Define the DP state clearly. Ask: "What is the minimum information I need to make a decision at each step?"
- Consider if you can reduce space by only keeping the last row/few values.
- LeetCode provides 3 hint(s) for this problem β try solving without them first.
π‘ Hints
Hint 1: 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].
Hint 2: Think of a 2-pointer approach to traverse the array and find each longest possible period.
Hint 3: 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.