[LeetCode] Number of Smooth Descent Periods of a Stock

2110. 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 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.

1
2
3
4
5
6
7
8
9
10
11
12
class Solution {
public:
long long getDescentPeriods(vector<int>& p) {
long long cnt = 1, res = 0;
for(int i = 0; i < p.size(); i++, cnt++) {
if(i != p.size() - 1 and p[i + 1] == p[i] - 1) continue;
res = res + (cnt + 1) * cnt / 2;
cnt = 0;
}
return res;
}
};
Author: Song Hayoung
Link: https://songhayoung.github.io/2022/02/01/PS/LeetCode/number-of-smooth-descent-periods-of-a-stock/
Copyright Notice: All articles in this blog are licensed under CC BY-NC-SA 4.0 unless stating additionally.