Number of Smooth Descent Periods of a Stock - 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 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.

Input & Output

Example 1 — Basic Descent
$ Input: prices = [3,2,1,4]
Output: 7
💡 Note: Smooth descent periods: [3], [2], [1], [4], [3,2], [2,1], [3,2,1]. Each single element forms a period, plus [3,2] (3→2 drops by 1), [2,1] (2→1 drops by 1), and [3,2,1] (both transitions drop by 1).
Example 2 — No Multi-Element Descents
$ Input: prices = [8,6,7,7]
Output: 4
💡 Note: Only single-element periods: [8], [6], [7], [7]. No multi-element descents since 8→6 drops by 2 (not 1), 6→7 increases, 7→7 stays same.
Example 3 — Long Descent
$ Input: prices = [1]
Output: 1
💡 Note: Single element [1] forms one descent period.

Constraints

  • 1 ≤ prices.length ≤ 105
  • 1 ≤ prices[i] ≤ 109

Visualization

Tap to expand
Smooth Descent Periods: Count Valid Subarrays3214price[0]price[1]price[2]price[3]-1-1+3Valid Descent Periods:[3] [2] [1] [4]← Single elements (4 periods)[3,2] [2,1]← Two-element descents (2 periods)[3,2,1]← Three-element descent (1 period)Total Smooth Descent Periods: 7
Understanding the Visualization
1
Input
Array of stock prices: [3,2,1,4]
2
Find Descents
Identify all valid smooth descent periods
3
Count Total
Return total number of descent periods: 7
Key Takeaway
🎯 Key Insight: A descent of length n contributes exactly n subarrays to the total count
Asked in
Google 25 Amazon 18 Facebook 15 Microsoft 12
28.5K Views
Medium Frequency
~15 min Avg. Time
892 Likes
Ln 1, Col 1
Smart Actions
💡 Explanation
AI Ready
💡 Suggestion Tab to accept Esc to dismiss
// Output will appear here after running code
Code Editor Closed
Click the red button to reopen