Advertisement
kosievdmerwe

Untitled

Nov 12th, 2021
94
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 0.96 KB | None | 0 0
  1. class Solution:
  2.     def dailyTemperatures(self, temperatures: List[int]) -> List[int]:
  3.         N = len(temperatures)
  4.         ans = [0] * N
  5.        
  6.         # monostack tuple[temp, -idx]
  7.         # Note we store the negative of the index to
  8.         # make comparisons easier.
  9.         s = []  
  10.         for i in range(N - 1, -1, -1):
  11.             t = temperatures[i]
  12.             while s and s[-1] < (t, -i):
  13.                 s.pop()
  14.            
  15.             if s:
  16.                 ans[i] = -s[-1][1] - i
  17.             s.append((t, -i))
  18.         return ans
  19.  
  20. class Solution:
  21.     def dailyTemperatures(self, temperatures: List[int]) -> List[int]:
  22.         N = len(temperatures)
  23.         ans = [0] * N
  24.        
  25.         s = []  
  26.         for i, t in enumerate(reversed(temperatures)):
  27.             while s and s[-1] < (t, i):
  28.                 s.pop()
  29.            
  30.             if s:
  31.                 ans[N - 1 - i] = i - s[-1][1]
  32.             s.append((t, i))
  33.         return ans
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement