Daily Temperatures

题目
Given a list of daily temperatures, produce a list that, for each day in the input, tells you how many days you would have to wait until a warmer temperature. If there is no future day for which this is possible, put 0 instead.

For example, given the list temperatures = [73, 74, 75, 71, 69, 72, 76, 73], your output should be [1, 1, 4, 2, 1, 1, 0, 0].

Note: The length of temperatures will be in the range [1, 30000]. Each temperature will be an integer in the range [30, 100].

答案

class Solution {
    public int[] dailyTemperatures(int[] temperatures) {
        int n = temperatures.length;
        int[] ans = new int[n];
        
        Stack<Integer> temp_stk = new Stack<>();
        Stack<Integer> idx_stk = new Stack<>();
        
        for(int i = 0; i < n; i++) {
            int curr_temp = temperatures[i];
            while(!temp_stk.empty() && temp_stk.peek() < curr_temp) {
                int t = temp_stk.pop();
                int idx = idx_stk.pop();
                ans[idx] = i - idx;
            }
            temp_stk.push(curr_temp);
            idx_stk.push(i);
        }
        return ans;        
    }
}
©著作权归作者所有,转载或内容合作请联系作者
【社区内容提示】社区部分内容疑似由AI辅助生成,浏览时请结合常识与多方信息审慎甄别。
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

相关阅读更多精彩内容

  • rljs by sennchi Timeline of History Part One The Cognitiv...
    sennchi阅读 12,196评论 0 10
  • 李笑来老师谈到了两种人生,一种是想学的人生,“”学了一辈子,应该说是想了一辈子,没有基本的学习能力,所以终身在原地...
    容Elsa阅读 3,434评论 6 5
  • 看到车外一闪而过的梧桐花,我回头趴在车窗上看了很久。那是温柔淡雅的粉色梧桐花,也有少量淡紫色的梧桐花,安安静静的散...
    自由在路上阅读 3,831评论 0 7
  • 以前好羡慕新一和小兰,虽然不能在一起,但是心在一起 能找到这样的人好难 并不是不祝福新兰 而是对他们这样初恋即一辈...
    18a06fda2d26阅读 1,535评论 1 0

友情链接更多精彩内容