#2110

Number of Smooth Descent Periods of a Stock

pupil · 595 · lc medium +29 · verified · 67.7% accepted · 1,076 likes · top 74%

Description

You are given an integer array prices representing the daily stock prices, where prices[i] is the price on day i.

A smooth descent period is a run of one or more consecutive days where each day's price is exactly 1 less than the previous day's price. A single day always qualifies as a smooth descent period.

Return the total count 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]

Code

1
2
3