classSolution:defdailyTemperatures(self, temperatures: List[int])-> List[int]:
n =len(temperatures)
res =[0]* n
stack =[]for i inrange(n-1,-1,-1):while stack and temperatures[stack[-1]]<= temperatures[i]:
stack.pop()if stack:
res[i]= stack[-1]- i
stack.append(i)return res
Java
classSolution{publicint[]dailyTemperatures(int[] temperatures){int n = temperatures.length;int[] res =newint[n];Stack<Integer> stack =newStack<>();for(int i = n -1; i >=0;--i){while(!stack.isEmpty()&& temperatures[i]>= temperatures[stack.peek()]){
stack.pop();}
res[i]= stack.isEmpty()?0: stack.peek()- i;
stack.push(i);}return res;}}