|
| 1 | +import java.util.Arrays; |
| 2 | +import java.util.ArrayDeque; |
| 3 | +import java.util.Deque; |
| 4 | + |
| 5 | +/** |
| 6 | + * MAANG screen drill: Daily Temperatures (monotonic decreasing stack). |
| 7 | + * |
| 8 | + * Interview signal: |
| 9 | + * - Do you recognize "next greater element" as a monotonic-stack pattern |
| 10 | + * instead of brute-force O(n^2) double loops? |
| 11 | + * - Do you store indices on the stack (not values), so you can compute the |
| 12 | + * distance between the warmer day and the day waiting for it? |
| 13 | + * |
| 14 | + * Problem: |
| 15 | + * Given an array of integer daily temperatures, return an array answer where |
| 16 | + * answer[i] is the number of days you have to wait after day i to get a |
| 17 | + * warmer temperature. If no future day is warmer, answer[i] = 0. |
| 18 | + * |
| 19 | + * Approach: |
| 20 | + * 1. Walk left to right keeping a stack of indices whose answer is unknown. |
| 21 | + * 2. While the current temperature is strictly greater than the temperature |
| 22 | + * at the top index, that top index has just found its warmer day -- pop |
| 23 | + * it and write the gap (currentIndex - poppedIndex) into answer. |
| 24 | + * 3. Push the current index. Stack stays monotonically decreasing in |
| 25 | + * temperature, so each index is pushed and popped at most once. |
| 26 | + * |
| 27 | + * Complexity: |
| 28 | + * - Time: O(n). Each index is pushed and popped at most once. |
| 29 | + * - Space: O(n) for the stack and the output array. |
| 30 | + */ |
| 31 | +public class DailyTemperatures { |
| 32 | + |
| 33 | + public static int[] dailyTemperatures(int[] temperatures) { |
| 34 | + if (temperatures == null || temperatures.length == 0) { |
| 35 | + return new int[0]; |
| 36 | + } |
| 37 | + |
| 38 | + int[] answer = new int[temperatures.length]; |
| 39 | + Deque<Integer> waitingIndices = new ArrayDeque<>(); |
| 40 | + |
| 41 | + for (int i = 0; i < temperatures.length; i++) { |
| 42 | + while (!waitingIndices.isEmpty() |
| 43 | + && temperatures[i] > temperatures[waitingIndices.peek()]) { |
| 44 | + int waitingIndex = waitingIndices.pop(); |
| 45 | + answer[waitingIndex] = i - waitingIndex; |
| 46 | + } |
| 47 | + waitingIndices.push(i); |
| 48 | + } |
| 49 | + |
| 50 | + return answer; |
| 51 | + } |
| 52 | + |
| 53 | + public static void main(String[] args) { |
| 54 | + expect(new int[] {1, 1, 4, 2, 1, 1, 0, 0}, |
| 55 | + dailyTemperatures(new int[] {73, 74, 75, 71, 69, 72, 76, 73}), |
| 56 | + "classic mixed warming and cooling stretch"); |
| 57 | + |
| 58 | + expect(new int[] {1, 1, 1, 0}, |
| 59 | + dailyTemperatures(new int[] {30, 40, 50, 60}), |
| 60 | + "strictly increasing -- every day waits one"); |
| 61 | + |
| 62 | + expect(new int[] {0, 0, 0, 0}, |
| 63 | + dailyTemperatures(new int[] {60, 50, 40, 30}), |
| 64 | + "strictly decreasing -- no warmer day ever"); |
| 65 | + |
| 66 | + expect(new int[] {0, 0, 0, 0}, |
| 67 | + dailyTemperatures(new int[] {50, 50, 50, 50}), |
| 68 | + "ties do not count -- strictly greater required"); |
| 69 | + |
| 70 | + expect(new int[] {0}, |
| 71 | + dailyTemperatures(new int[] {99}), |
| 72 | + "single day has no future"); |
| 73 | + |
| 74 | + expect(new int[0], |
| 75 | + dailyTemperatures(new int[0]), |
| 76 | + "empty input returns empty answer"); |
| 77 | + |
| 78 | + System.out.println("All DailyTemperatures drill checks passed."); |
| 79 | + } |
| 80 | + |
| 81 | + private static void expect(int[] expected, int[] actual, String label) { |
| 82 | + if (!Arrays.equals(expected, actual)) { |
| 83 | + throw new AssertionError(label |
| 84 | + + " expected " + Arrays.toString(expected) |
| 85 | + + " but got " + Arrays.toString(actual)); |
| 86 | + } |
| 87 | + } |
| 88 | +} |
0 commit comments