forked from HarshCasper/NeoAlgo
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathstock_span.py
More file actions
41 lines (29 loc) · 691 Bytes
/
stock_span.py
File metadata and controls
41 lines (29 loc) · 691 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
''' Stock Span Problem
Given a list of prices of a stock for N number of days,
find stock span for each day.
'''
def calcSpan(price):
day = len(price)
stack = []
span = [0 for i in range(0, day)]
span[0] = 1
stack.append(0)
for i in range(1, day):
while (len(stack) > 0 and price[stack[-1]] <= price[i]):
stack.pop()
if len(stack) <= 0:
span[i] = i + 1
else:
span[i] = i - stack[-1]
stack.append(i)
print(span)
prices = list(map(int, input().strip().split()))
calcSpan(prices)
'''
sample Input:
10 30 20 50 20
sample output:
1 2 1 4 1
Time complexity: O(n)
space complexity:O(n)
'''