Number of Smooth Descent Periods of a Stock
MediumUpdated: Aug 2, 2025
Practice on:
Problem
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 ofsmooth descent periods.
Examples
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^51 <= prices[i] <= 10^5
Solution
Method 1 – Dynamic Programming (Count Lengths)
Intuition
Every single day is a smooth descent period. For longer periods, if the current price is exactly 1 less than the previous, we can extend the previous descent period. We keep track of the length of the current descent and sum up all such periods.
Approach
- Initialize
ansto 1 (for the first day) andcurto 1 (current descent length). - For each day from the second onward:
- If
prices[i] == prices[i-1] - 1, incrementcur. - Else, reset
curto 1. - Add
curtoans.
- If
- Return
ans.
Code
C++
class Solution {
public:
long long getDescentPeriods(vector<int>& prices) {
long long ans = 1, cur = 1;
for (int i = 1; i < prices.size(); ++i) {
if (prices[i] == prices[i-1] - 1) ++cur;
else cur = 1;
ans += cur;
}
return ans;
}
};
Go
func getDescentPeriods(prices []int) int64 {
ans, cur := int64(1), int64(1)
for i := 1; i < len(prices); i++ {
if prices[i] == prices[i-1]-1 {
cur++
} else {
cur = 1
}
ans += cur
}
return ans
}
Java
class Solution {
public long getDescentPeriods(int[] prices) {
long ans = 1, cur = 1;
for (int i = 1; i < prices.length; ++i) {
if (prices[i] == prices[i-1] - 1) ++cur;
else cur = 1;
ans += cur;
}
return ans;
}
}
Kotlin
class Solution {
fun getDescentPeriods(prices: IntArray): Long {
var ans = 1L
var cur = 1L
for (i in 1 until prices.size) {
if (prices[i] == prices[i-1] - 1) cur++
else cur = 1L
ans += cur
}
return ans
}
}
Python
class Solution:
def getDescentPeriods(self, prices: list[int]) -> int:
ans = cur = 1
for i in range(1, len(prices)):
if prices[i] == prices[i-1] - 1:
cur += 1
else:
cur = 1
ans += cur
return ans
Rust
impl Solution {
pub fn get_descent_periods(prices: Vec<i32>) -> i64 {
let mut ans = 1i64;
let mut cur = 1i64;
for i in 1..prices.len() {
if prices[i] == prices[i-1] - 1 {
cur += 1;
} else {
cur = 1;
}
ans += cur;
}
ans
}
}
TypeScript
class Solution {
getDescentPeriods(prices: number[]): number {
let ans = 1, cur = 1;
for (let i = 1; i < prices.length; ++i) {
if (prices[i] === prices[i-1] - 1) cur++;
else cur = 1;
ans += cur;
}
return ans;
}
}
Complexity
- ⏰ Time complexity:
O(n), wherenis the length ofprices. We scan the array once. - 🧺 Space complexity:
O(1), as we use only a constant amount of extra space.