We read every piece of feedback, and take your input very seriously.
To see all available qualifiers, see our documentation.
There was an error while loading. Please reload this page.
1 parent 8a5557c commit 6dafa54Copy full SHA for 6dafa54
data_structures/stacks/stock_span_problem.py
@@ -42,14 +42,14 @@ def calculate_span(price: list[int]) -> list[int]:
42
for i in range(1, n):
43
# Pop elements from stack while stack is not
44
# empty and top of stack is smaller than price[i]
45
- while len(st) > 0 and price[st[0]] <= price[i]:
+ while len(st) > 0 and price[st[-1]] <= price[i]:
46
st.pop()
47
48
# If stack becomes empty, then price[i] is greater
49
# than all elements on left of it, i.e. price[0],
50
# price[1], ..price[i-1]. Else the price[i] is
51
# greater than elements after top of stack
52
- s[i] = i + 1 if len(st) <= 0 else (i - st[0])
+ s[i] = i + 1 if len(st) <= 0 else (i - st[-1])
53
54
# Push this element to stack
55
st.append(i)
0 commit comments