739. 每日温度

class Solution {
public int[] dailyTemperatures(int[] T) {
if (T == null || T.length == 0) return new int[]{};
Stack minStack = new Stack<>();
int[] res = new int[T.length];
minStack.push(0);
for(int i = 1; i < T.length;i++){
while (!minStack.isEmpty() && T[minStack.peek()] < T[i]){
int daybefore = minStack.pop();
res[daybefore] = i - daybefore;
}
minStack.push(i);
}
return res;

    //维护一个递减栈
    //上一版的代码超时原因在于将天数一天天累加增加了复杂度 实际上数组索引已经代表了天数 题目还是做太少了

}

}

posted @ 2021-01-02 17:26  backTraced  阅读(50)  评论(0)    收藏  举报