Number of Smooth Descent Periods of a Stock LeetCode solution in Python

1
import sys
2

3

4
class Solution:
5
def getDescentPeriods(self, prices: List[int]) -> int:
6
def calculate(k, ans):
7
if k > 1:
8
ans += ((k - 1) * (k)) // 2
9
# Sum of Natural Numbers
10
return ans
11
else:
12
return ans
13

14
end = 0
15
start = 0
16
prev = sys.maxsize
17
k = 0
18
ans = 0
19
while end < len(prices):
20
if prev - prices[end] == 1 or prev == sys.maxsize:
21
k += 1
22
prev = prices[end]
23
end += 1
24
else:
25
ans = calculate(k, ans)
26
start = end
27
if end < len(prices):
28
prev = sys.maxsize
29
k = 0
30
if k > 1:
31
ans = calculate(k, ans)
32

33
return ans + len(prices)

0

WPM •0 •0

100%

ACC •0 •0

0s

TIME •0