-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy path739.py
More file actions
22 lines (18 loc) · 889 Bytes
/
739.py
File metadata and controls
22 lines (18 loc) · 889 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
class Solution:
def dailyTemperatures(self, temperatures: List[int]) -> List[int]:
soln = [0 for i in range(len(temperatures))]
# theory: whenever stack gets filled with more numbers, numbers can
# only go down, so when things get popped, it works out!
# ***conjecture*** :sparkles: lowest element will always be at top of stack
tempStack = []
for i in range(len(temperatures)):
if (len(tempStack) == 0):
tempStack.append(i) # store indicies
elif (temperatures[i] > temperatures[tempStack[-1]]):
while (len(tempStack) > 0 and temperatures[i] > temperatures[tempStack[-1]]):
lastEle = tempStack.pop()
soln[lastEle] = i - lastEle
tempStack.append(i)
else:
tempStack.append(i)
return soln