|
| 1 | + |
| 2 | +# Monotonic Stack & LeetCode's "Daily Temperatures" |
| 3 | + |
| 4 | +Let's dive into a cool data structure called the **Monotonic Stack** and see how it helps us solve a popular coding problem, "Daily Temperatures". |
| 5 | + |
| 6 | +## What's a Monotonic Stack? |
| 7 | + |
| 8 | +Imagine a stack of books, but with a rule: you can only place a new book on top if it's lighter than the book already there. This creates a stack of books sorted by weight, from heaviest at the bottom to lightest at the top. |
| 9 | + |
| 10 | +That's a **monotonic stack**. It's a regular stack, but it enforces a specific order on its elements – either always increasing or always decreasing. |
| 11 | + |
| 12 | +* **Monotonically Increasing Stack:** Elements are always in increasing order from bottom to top. `[1, 2, 5, 8]` |
| 13 | +* **Monotonically Decreasing Stack:** Elements are always in decreasing order from bottom to top. `[10, 7, 4, 1]` |
| 14 | + |
| 15 | +This simple rule makes them incredibly efficient for problems where you need to find the "next greater element" or "previous smaller element" for items in a sequence. |
| 16 | + |
| 17 | +--- |
| 18 | + |
| 19 | +## The Problem: Daily Temperatures (LeetCode 739) |
| 20 | + |
| 21 | +The problem is this: You're given a list of daily temperatures. For each day, you need to figure out how many days you have to wait for a warmer temperature. If no such day exists, the answer is 0. |
| 22 | + |
| 23 | +**Example:** |
| 24 | + |
| 25 | +`temperatures = [73, 74, 75, 71, 69, 72, 76, 73]` |
| 26 | + |
| 27 | +**Expected Output:** |
| 28 | + |
| 29 | +`result = [1, 1, 4, 2, 1, 1, 0, 0]` |
| 30 | + |
| 31 | +Let's break down why: |
| 32 | +- For day 0 (73°), the next day (day 1) is warmer (74°). So, wait is `1` day. |
| 33 | +- For day 1 (74°), the next day (day 2) is warmer (75°). So, wait is `1` day. |
| 34 | +- For day 2 (75°), you have to wait until day 6 (76°). So, wait is `6 - 2 = 4` days. |
| 35 | +- ...and so on. |
| 36 | + |
| 37 | +--- |
| 38 | + |
| 39 | +## The Solution: Using a Monotonically Decreasing Stack |
| 40 | + |
| 41 | +We'll use a stack to store the *indices* of the days. We'll keep this stack monotonically decreasing, meaning the temperatures corresponding to the indices in the stack will always be going down. |
| 42 | + |
| 43 | +Let's trace our example: `temperatures = [73, 74, 75, 71, 69, 72, 76, 73]` |
| 44 | + |
| 45 | +Initialize `result = [0, 0, 0, 0, 0, 0, 0, 0]` and an empty `stack`. |
| 46 | + |
| 47 | +**Day 0: Temp = 73** |
| 48 | +- Stack is empty. Push index `0`. |
| 49 | + |
| 50 | +``` |
| 51 | +Temperatures: [73, 74, 75, 71, 69, 72, 76, 73] |
| 52 | + ^ (Current: 73) |
| 53 | +Stack: |
| 54 | +| 0 | (Temp: 73) |
| 55 | ++---+ |
| 56 | +``` |
| 57 | + |
| 58 | +**Day 1: Temp = 74** |
| 59 | +- `74` is warmer than `temperatures[0]` (73). |
| 60 | +- We found a warmer day for index `0`! |
| 61 | +- Pop `0` from the stack. |
| 62 | +- `result[0] = 1 (current index) - 0 (popped index) = 1`. |
| 63 | +- Now the stack is empty. Push index `1`. |
| 64 | + |
| 65 | +``` |
| 66 | +Temperatures: [73, 74, 75, 71, 69, 72, 76, 73] |
| 67 | + ^ (Current: 74) |
| 68 | +Stack: |
| 69 | +| 1 | (Temp: 74) |
| 70 | ++---+ |
| 71 | + |
| 72 | +Result: [1, 0, 0, 0, 0, 0, 0, 0] |
| 73 | +``` |
| 74 | + |
| 75 | +**Day 2: Temp = 75** |
| 76 | +- `75` is warmer than `temperatures[1]` (74). |
| 77 | +- Pop `1`. `result[1] = 2 - 1 = 1`. |
| 78 | +- Stack is empty. Push index `2`. |
| 79 | + |
| 80 | +``` |
| 81 | +Temperatures: [73, 74, 75, 71, 69, 72, 76, 73] |
| 82 | + ^ (Current: 75) |
| 83 | +Stack: |
| 84 | +| 2 | (Temp: 75) |
| 85 | ++---+ |
| 86 | + |
| 87 | +Result: [1, 1, 0, 0, 0, 0, 0, 0] |
| 88 | +``` |
| 89 | + |
| 90 | +**Day 3: Temp = 71** |
| 91 | +- `71` is not warmer than `temperatures[2]` (75). |
| 92 | +- The stack needs to stay decreasing. Push index `3`. |
| 93 | + |
| 94 | +``` |
| 95 | +Temperatures: [73, 74, 75, 71, 69, 72, 76, 73] |
| 96 | + ^ (Current: 71) |
| 97 | +Stack: |
| 98 | +| 3 | (Temp: 71) |
| 99 | +| 2 | (Temp: 75) |
| 100 | ++---+ |
| 101 | +``` |
| 102 | + |
| 103 | +**Day 4: Temp = 69** |
| 104 | +- `69` is not warmer than `temperatures[3]` (71). |
| 105 | +- Push index `4`. |
| 106 | + |
| 107 | +``` |
| 108 | +Temperatures: [73, 74, 75, 71, 69, 72, 76, 73] |
| 109 | + ^ (Current: 69) |
| 110 | +Stack: |
| 111 | +| 4 | (Temp: 69) |
| 112 | +| 3 | (Temp: 71) |
| 113 | +| 2 | (Temp: 75) |
| 114 | ++---+ |
| 115 | +``` |
| 116 | + |
| 117 | +**Day 5: Temp = 72** |
| 118 | +- `72` is warmer than `temperatures[4]` (69). |
| 119 | +- Pop `4`. `result[4] = 5 - 4 = 1`. |
| 120 | +- `72` is warmer than `temperatures[3]` (71). |
| 121 | +- Pop `3`. `result[3] = 5 - 3 = 2`. |
| 122 | +- `72` is NOT warmer than `temperatures[2]` (75). Stop popping. |
| 123 | +- Push index `5`. |
| 124 | + |
| 125 | +``` |
| 126 | +Temperatures: [73, 74, 75, 71, 69, 72, 76, 73] |
| 127 | + ^ (Current: 72) |
| 128 | +Stack: |
| 129 | +| 5 | (Temp: 72) |
| 130 | +| 2 | (Temp: 75) |
| 131 | ++---+ |
| 132 | + |
| 133 | +Result: [1, 1, 0, 2, 1, 0, 0, 0] |
| 134 | +``` |
| 135 | + |
| 136 | +**Day 6: Temp = 76** |
| 137 | +- `76` is warmer than `temperatures[5]` (72). |
| 138 | +- Pop `5`. `result[5] = 6 - 5 = 1`. |
| 139 | +- `76` is warmer than `temperatures[2]` (75). |
| 140 | +- Pop `2`. `result[2] = 6 - 2 = 4`. |
| 141 | +- Stack is empty. Push index `6`. |
| 142 | + |
| 143 | +``` |
| 144 | +Temperatures: [73, 74, 75, 71, 69, 72, 76, 73] |
| 145 | + ^ (Current: 76) |
| 146 | +Stack: |
| 147 | +| 6 | (Temp: 76) |
| 148 | ++---+ |
| 149 | + |
| 150 | +Result: [1, 1, 4, 2, 1, 1, 0, 0] |
| 151 | +``` |
| 152 | + |
| 153 | +**Day 7: Temp = 73** |
| 154 | +- `73` is not warmer than `temperatures[6]` (76). |
| 155 | +- Push index `7`. |
| 156 | + |
| 157 | +``` |
| 158 | +Temperatures: [73, 74, 75, 71, 69, 72, 76, 73] |
| 159 | + ^ (Current: 73) |
| 160 | +Stack: |
| 161 | +| 7 | (Temp: 73) |
| 162 | +| 6 | (Temp: 76) |
| 163 | ++---+ |
| 164 | +``` |
| 165 | + |
| 166 | +**End of Loop** |
| 167 | +- The loop finishes. Any indices left in the stack (`6` and `7`) don't have a warmer day after them, so their results remain `0`. |
| 168 | + |
| 169 | +**Final Result:** `[1, 1, 4, 2, 1, 1, 0, 0]` |
| 170 | + |
| 171 | +--- |
| 172 | + |
| 173 | +### Code Example (JavaScript) |
| 174 | + |
| 175 | +```javascript |
| 176 | +function dailyTemperatures(temperatures) { |
| 177 | + const result = new Array(temperatures.length).fill(0); |
| 178 | + const stack = []; // We'll store indices here |
| 179 | + |
| 180 | + for (let i = 0; i < temperatures.length; i++) { |
| 181 | + // While stack is not empty AND current temp is warmer than the temp at the index on top of the stack |
| 182 | + while (stack.length > 0 && temperatures[i] > temperatures[stack[stack.length - 1]]) { |
| 183 | + const prevIndex = stack.pop(); |
| 184 | + result[prevIndex] = i - prevIndex; |
| 185 | + } |
| 186 | + // Push the current index onto the stack |
| 187 | + stack.push(i); |
| 188 | + } |
| 189 | + |
| 190 | + return result; |
| 191 | +} |
| 192 | +``` |
| 193 | + |
| 194 | +### Code Example (GoLang) |
| 195 | + |
| 196 | +```go |
| 197 | +func dailyTemperatures(temperatures []int) []int { |
| 198 | + n := len(temperatures) |
| 199 | + result := make([]int, n) |
| 200 | + stack := []int{} // We'll store indices here |
| 201 | + |
| 202 | + for i := 0; i < n; i++ { |
| 203 | + // While stack is not empty AND current temp is warmer than the temp at the index on top of the stack |
| 204 | + for len(stack) > 0 && temperatures[i] > temperatures[stack[len(stack)-1]] { |
| 205 | + prevIndex := stack[len(stack)-1] |
| 206 | + stack = stack[:len(stack)-1] // Pop |
| 207 | + result[prevIndex] = i - prevIndex |
| 208 | + } |
| 209 | + // Push the current index onto the stack |
| 210 | + stack = append(stack, i) |
| 211 | + } |
| 212 | + |
| 213 | + return result |
| 214 | +} |
| 215 | +``` |
| 216 | + |
| 217 | +And that's how a monotonic stack gives us an elegant and efficient solution! |
0 commit comments