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;        
    }
}
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時請結(jié)合常識與多方信息審慎甄別。
平臺聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點,簡書系信息發(fā)布平臺,僅提供信息存儲服務(wù)。

相關(guān)閱讀更多精彩內(nèi)容

  • rljs by sennchi Timeline of History Part One The Cognitiv...
    sennchi閱讀 7,854評論 0 10
  • 李笑來老師談到了兩種人生,一種是想學(xué)的人生,“”學(xué)了一輩子,應(yīng)該說是想了一輩子,沒有基本的學(xué)習(xí)能力,所以終身在原地...
    容Elsa閱讀 558評論 6 5
  • 看到車外一閃而過的梧桐花,我回頭趴在車窗上看了很久。那是溫柔淡雅的粉色梧桐花,也有少量淡紫色的梧桐花,安安靜靜的散...
    自由在路上閱讀 692評論 0 7
  • 以前好羨慕新一和小蘭,雖然不能在一起,但是心在一起 能找到這樣的人好難 并不是不祝福新蘭 而是對他們這樣初戀即一輩...
    18a06fda2d26閱讀 224評論 1 0

友情鏈接更多精彩內(nèi)容