Skip to content

Commit 3b6a0fd

Browse files
committed
Updates
1 parent 36a0a7e commit 3b6a0fd

File tree

4 files changed

+239
-3
lines changed

4 files changed

+239
-3
lines changed

posts/algos/monotonic-stack.txt

Lines changed: 217 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,217 @@
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!

posts/weighted-quick-union-with-path-compression.txt renamed to posts/algos/weighted-quick-union-with-path-compression.txt

File renamed without changes.

posts/posts.json

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,14 +7,23 @@
77
"tags": ["cs", "algorithms", "graphs"],
88
"series": {
99
"posts": [
10+
{
11+
"slug": "monotonic-stack",
12+
"title": "Monotonic Stack with Daily Temperatures",
13+
"date": "2025-11-05",
14+
"description": "Monotonic Stack with Daily Temperatures",
15+
"tags": ["cs", "algorithms", "stack", "monotonic-stack"],
16+
"category": "dev",
17+
"filename": "/algos/monotonic-stack.txt"
18+
},
1019
{
1120
"slug": "wquwpc",
1221
"title": "Weighted Quick-Union with Path Compression",
1322
"date": "2025-11-04",
1423
"description": "Weighted Quick-Union with Path Compression",
1524
"tags": ["cs", "algorithms", "graphs"],
1625
"category": "dev",
17-
"filename": "weighted-quick-union-with-path-compression.txt"
26+
"filename": "/algos/weighted-quick-union-with-path-compression.txt"
1827
}
1928
]
2029
}

rss.xml

Lines changed: 12 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -9,14 +9,24 @@
99
<link>https://fezcode.com</link>
1010
</image>
1111
<generator>RSS for Node</generator>
12-
<lastBuildDate>Tue, 04 Nov 2025 18:55:10 GMT</lastBuildDate>
12+
<lastBuildDate>Tue, 04 Nov 2025 22:07:04 GMT</lastBuildDate>
1313
<atom:link href="https://fezcode.com/rss.xml" rel="self" type="application/rss+xml"/>
14-
<pubDate>Tue, 04 Nov 2025 18:55:10 GMT</pubDate>
14+
<pubDate>Tue, 04 Nov 2025 22:07:04 GMT</pubDate>
1515
<copyright><![CDATA[2025 Ahmed Samil Bulbul]]></copyright>
1616
<language><![CDATA[en]]></language>
1717
<managingEditor><![CDATA[samil.bulbul@gmail.com (Ahmed Samil Bulbul)]]></managingEditor>
1818
<webMaster><![CDATA[samil.bulbul@gmail.com (Ahmed Samil Bulbul)]]></webMaster>
1919
<ttl>60</ttl>
20+
<item>
21+
<title><![CDATA[Monotonic Stack with Daily Temperatures]]></title>
22+
<description><![CDATA[[object Object]]]></description>
23+
<link>https://fezcode.com/#/blog/monotonic-stack</link>
24+
<guid isPermaLink="false">https://fezcode.com/#/blog/monotonic-stack</guid>
25+
<dc:creator><![CDATA[Ahmed Samil Bulbul]]></dc:creator>
26+
<pubDate>Wed, 05 Nov 2025 00:00:00 GMT</pubDate>
27+
<content:encoded><![CDATA[<h1>Monotonic Stack &amp; LeetCode&#39;s &quot;Daily Temperatures&quot;</h1>
28+
<p><a href="https://fezcode.com/#/blog/monotonic-stack">Read more...</a></p>]]></content:encoded>
29+
</item>
2030
<item>
2131
<title><![CDATA[Weighted Quick-Union with Path Compression]]></title>
2232
<description><![CDATA[[object Object]]]></description>

0 commit comments

Comments
 (0)