Number of Smooth Descent Periods of a Stock

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
Inputprices = [3,2,1,4]
Output7
There are 7 smooth descent periods: [3], [2], [1], [4], [3, 2], [2, 1], and [3, 2, 1].
Example 2
Inputprices = [8,6,7,7]
Output4
There are 4 smooth descent periods: [8], [6], [7], and [7], while [8, 6] is not smooth because 8 - 6  1.

Constraints

  • 1 <= prices.length <= 10^5
  • 1 <= prices[i] <= 10^5

Asked at 3 companies

</>

Your Solution

(Ctrl/Cmd + Enter)

Switching Language

Loading template...

Loading...

Sign in to save your progress

AI code evaluation

Get a correctness verdict, missed edge cases, and complexity analysis of your solution.

Sign in to evaluate