Need help in python. This code is from leetcode.com, one of the solutions for this problem and cannot understand conditional statement there, exactly the code "stack[-1][1]
"
class Solution(object):
def dailyTemperatures(self, temps):
if not temps:
return []
result = [0] * len(temps)
stack = []
for curr_idx, curr_temp in enumerate(temps):
while stack and curr_temp > stack[-1][1]: # not clear, and I know, it is not a type of access to list element
last_idx, last_temp = stack.pop()
result[last_idx] = curr_idx - last_idx
stack.append((curr_idx, curr_temp))
return result
It is returning the second element of whatever is in the last index of stack
for instance, if the stack was a list of strings like
stack = ['abc','def','ghi']
than stack[-1][1] returns
stack[-1] <-- 'ghi'
stack[-1][1] <-- 'h'