Guest User

Untitled

a guest
Sep 22nd, 2018
75
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 0.79 KB | None | 0 0
  1. # Python3
  2.  
  3. class Solution:
  4. def dailyTemperatures(self, temperatures):
  5. """
  6. :type temperatures: List[int]
  7. :rtype: List[int]
  8. """
  9.  
  10. # approach: use a stack for saving decreasing temperatures
  11. # and update their distance once there is a
  12. # temperature is greater than top of stack
  13.  
  14. n = len(temperatures)
  15. stack = []
  16. result = [0] * n
  17.  
  18. for i in range(n):
  19. current_temperature = temperatures[i]
  20.  
  21. # pop all temperatures in stack out and update indices
  22. while len(stack) and current_temperature > temperatures[stack[-1]]:
  23. item_index = stack.pop()
  24. result[item_index] = i - item_index
  25.  
  26. stack.append(i)
  27.  
  28. return result
Add Comment
Please, Sign In to add comment