Skip to main content
Back to problems
#2110
Medium Algorithms

Number of smooth descent periods of a stock

Array Math Two Pointers Dynamic Programming Sliding Window
67.7% acceptance
Feb 25, 2026
1076
53
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.

Solution

Rust
Time O(n)
Space O(1)
LeetCode
solution.rs
impl Solution {
  pub fn get_descent_periods(prices: Vec<i32>) -> i64 {
    let mut ans = 0i64;
    let mut run = 0i64;
    for i in 0..prices.len() {
      if i > 0 && prices[i] == prices[i - 1] - 1 {
        run += 1;
      } else {
        run = 1;
      }
      ans += run;
    }
    ans
  }
}