lc-739


/**
Given an array of integers temperatures represents the daily temperatures, 
return an array answer such that answer[i] is the number of days you have to wait 
after the iᵗʰ day to get a warmer temperature. If there is no future day for which 
this is possible, keep answer[i] == 0 instead. 

 
 Example 1: 
 Input: temperatures = [73,74,75,71,69,72,76,73]
Output: [1,1,4,2,1,1,0,0]
 Example 2: 
 Input: temperatures = [30,40,50,60]
Output: [1,1,1,0]
 Example 3: 
 Input: temperatures = [30,60,90]
Output: [1,1,0]
 
 
 Constraints: 

 
 1 <= temperatures.length <= 10⁵ 
 30 <= temperatures[i] <= 100 
 
 Related Topics栈 | 数组 | 单调栈 

 👍 1343, 👎 0 

*/
//leetcode submit region begin(Prohibit modification and deletion)
class Solution {
    class Temperature {
        int day;
        int value;
    }

    public int[] dailyTemperatures(int[] temperatures) {
        if (temperatures == null || temperatures.length == 0) {
            return temperatures;
        }
        Stack<Temperature> tStack = new Stack<>();
        int[] result = new int[temperatures.length];

        for (int i = 0; i < temperatures.length; i++) {
            while(!tStack.isEmpty()) {
                if (tStack.peek().value < temperatures[i]) {
                    Temperature t = tStack.pop();
                    result[t.day] = i - t.day;
                } else {
                    break;
                }
            }
            Temperature nt = new Temperature();
            nt.day = i;
            nt.value = temperatures[i];
            tStack.push(nt);
        }

        return result;
    }
}
//leetcode submit region end(Prohibit modification and deletion)

文章作者: 倪春恩
版权声明: 本博客所有文章除特別声明外,均采用 CC BY 4.0 许可协议。转载请注明来源 倪春恩 !