|
| 1 | +/* |
| 2 | + * @lc app=leetcode id=739 lang=java |
| 3 | + * |
| 4 | + * [739] Daily Temperatures |
| 5 | + * |
| 6 | + * https://leetcode.com/problems/daily-temperatures/description/ |
| 7 | + * |
| 8 | + * algorithms |
| 9 | + * Medium (59.10%) |
| 10 | + * Total Accepted: 53.6K |
| 11 | + * Total Submissions: 90.3K |
| 12 | + * Testcase Example: '[73,74,75,71,69,72,76,73]' |
| 13 | + * |
| 14 | + * |
| 15 | + * Given a list of daily temperatures T, return a list such that, for each day |
| 16 | + * in the input, tells you how many days you would have to wait until a warmer |
| 17 | + * temperature. If there is no future day for which this is possible, put 0 |
| 18 | + * instead. |
| 19 | + * |
| 20 | + * For example, given the list of temperatures T = [73, 74, 75, 71, 69, 72, 76, |
| 21 | + * 73], your output should be [1, 1, 4, 2, 1, 1, 0, 0]. |
| 22 | + * |
| 23 | + * |
| 24 | + * Note: |
| 25 | + * The length of temperatures will be in the range [1, 30000]. |
| 26 | + * Each temperature will be an integer in the range [30, 100]. |
| 27 | + * |
| 28 | + */ |
| 29 | +class Solution { |
| 30 | + public int[] dailyTemperatures(int[] T) { |
| 31 | + Stack<Integer> stack = new Stack<>(); |
| 32 | + int[] ret = new int[T.length]; |
| 33 | + for (int i = 0; i < T.length; i++) { |
| 34 | + while(!stack.empty() && T[i] > T[stack.peek()]) { |
| 35 | + int index = stack.pop(); |
| 36 | + ret[index] = i - index; |
| 37 | + } |
| 38 | + stack.push(i); |
| 39 | + } |
| 40 | + return ret; |
| 41 | + } |
| 42 | +} |
| 43 | + |
0 commit comments