diff options
author | Jack Sangdahl <jack@pngu.org> | 2024-07-03 12:30:09 -0600 |
---|---|---|
committer | Jack Sangdahl <jack@pngu.org> | 2024-07-03 12:30:09 -0600 |
commit | d9929fb2bd46e35c3372a26194e869b0cb078bf4 (patch) | |
tree | 17ec73ac03264074cbd5e188bcddb942cd3fbb2f /num_smooth_descent_periods_stock.py | |
parent | 318d09b755ea3fafd3dc75485dd9387ae777135c (diff) | |
download | leetcode-d9929fb2bd46e35c3372a26194e869b0cb078bf4.tar.gz leetcode-d9929fb2bd46e35c3372a26194e869b0cb078bf4.zip |
2110. number smooth descent periods of stock
Diffstat (limited to 'num_smooth_descent_periods_stock.py')
-rw-r--r-- | num_smooth_descent_periods_stock.py | 30 |
1 files changed, 30 insertions, 0 deletions
diff --git a/num_smooth_descent_periods_stock.py b/num_smooth_descent_periods_stock.py new file mode 100644 index 0000000..2445bde --- /dev/null +++ b/num_smooth_descent_periods_stock.py @@ -0,0 +1,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])) |