Advertisement
nathanwailes

LeetCode 739 - Daily Temperatures - 2023.10.31 solution

Oct 31st, 2023
982
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 0.96 KB | None | 0 0
  1. StackEntry = namedtuple("StackEntry", "value index")
  2. class Solution:
  3.     def dailyTemperatures(self, temperatures: List[int]) -> List[int]:
  4.         """ Solution: monotonically-increasing stack
  5.        """
  6.         stack = []
  7.         output = []
  8.         for i in range(len(temperatures)-1, -1, -1):
  9.             if len(stack) == 0:
  10.                 output.append(0)
  11.                 stack.append(StackEntry(value=temperatures[i], index=i))
  12.             else:
  13.                 current_temp = temperatures[i]
  14.                 while len(stack) > 0:
  15.                     if current_temp >= stack[-1].value:
  16.                         stack.pop()
  17.                     else:
  18.                         break
  19.                
  20.                 if len(stack) > 0:
  21.                     output.append(stack[-1].index - i)
  22.                 else:
  23.                     output.append(0)
  24.                 stack.append(StackEntry(value=temperatures[i], index=i))
  25.         return reversed(output)
  26.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement