Skip to content

Commit 6dafa54

Browse files
Update stock_span_problem.py
1 parent 8a5557c commit 6dafa54

File tree

1 file changed

+2
-2
lines changed

1 file changed

+2
-2
lines changed

data_structures/stacks/stock_span_problem.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -42,14 +42,14 @@ def calculate_span(price: list[int]) -> list[int]:
4242
for i in range(1, n):
4343
# Pop elements from stack while stack is not
4444
# empty and top of stack is smaller than price[i]
45-
while len(st) > 0 and price[st[0]] <= price[i]:
45+
while len(st) > 0 and price[st[-1]] <= price[i]:
4646
st.pop()
4747

4848
# If stack becomes empty, then price[i] is greater
4949
# than all elements on left of it, i.e. price[0],
5050
# price[1], ..price[i-1]. Else the price[i] is
5151
# greater than elements after top of stack
52-
s[i] = i + 1 if len(st) <= 0 else (i - st[0])
52+
s[i] = i + 1 if len(st) <= 0 else (i - st[-1])
5353

5454
# Push this element to stack
5555
st.append(i)

0 commit comments

Comments
 (0)