blob: 2445bde8a86d981fb8564a448eb7589844297d29 (
plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
|
# 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 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 peceding day by
exactly 1. the first day of the period is exempted from this rule. return the
number of smooth descent periods.
"""
class Solution(object):
def getDescentPeriods(self, prices):
"""
:type prices: List[int]
:rtype: int
"""
dp = [1] * len(prices)
for i in range(1, len(prices)):
if prices[i] == prices[i - 1] - 1:
dp[i] += dp[i - 1]
return sum(dp)
if __name__ == "__main__":
obj = Solution()
print(obj.getDescentPeriods(prices=[3, 2, 1, 4]))
print(obj.getDescentPeriods(prices=[8, 6, 7, 7]))
print(obj.getDescentPeriods(prices=[1]))
|