Skip to content

Commit 3ba3cf7

Browse files
Update stock_span_problem.py
1 parent c76afc8 commit 3ba3cf7

File tree

1 file changed

+8
-6
lines changed

1 file changed

+8
-6
lines changed

data_structures/stacks/stock_span_problem.py

Lines changed: 8 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -16,28 +16,30 @@ def calculation_span(price: list[float]) -> list[float]:
1616
Returns:
1717
>>> price = [10, 4, 5, 90, 120, 80]
1818
>>> calculation_span(price)
19-
[1.0, 1.0, 2.0, 4.0, 5.0, 6.0]
19+
[1.0, 1.0, 2.0, 4.0, 5.0, 1.0]
2020
>>> price = [100, 50, 60, 70, 80, 90]
2121
>>> calculation_span(price)
2222
[1.0, 1.0, 2.0, 3.0, 4.0, 5.0]
2323
>>> price = [5, 4, 3, 2, 1]
2424
>>> calculation_span(price)
25-
[1.0, 1.0, 2.0, 3.0, 4.0]
25+
[1.0, 1.0, 1.0, 1.0, 1.0]
2626
>>> price = [1, 2, 3, 4, 5]
2727
>>> calculation_span(price)
2828
[1.0, 2.0, 3.0, 4.0, 5.0]
2929
>>> price = [10, 20, 30, 40, 50]
3030
>>> calculation_span(price)
3131
[1.0, 2.0, 3.0, 4.0, 5.0]
32+
>>> calculation_span(price=[100, 80, 60, 70, 60, 75, 85])
33+
[1.0, 1.0, 1.0, 2.0, 1.0, 4.0, 6.0]
3234
"""
3335
n = len(price)
3436
st = [0]
35-
s = [0.0] * n
36-
s[0] = 1.0
37+
s = [1.0]
3738
for i in range(1, n):
38-
while len(st) > 0 and price[st[0]] <= price[i]:
39+
while st and price[st[-1]] <= price[i]:
3940
st.pop()
40-
s[i] = float(i + 1) if len(st) <= 0 else float(i - st[0])
41+
s.append(float(i - st[-1] if st else i + 1))
42+
st.append(i)
4143
return s
4244

4345

0 commit comments

Comments
 (0)