-
Notifications
You must be signed in to change notification settings - Fork 4
Expand file tree
/
Copy path02_MonotonicStack.cpp
More file actions
469 lines (392 loc) · 14.2 KB
/
Copy path02_MonotonicStack.cpp
File metadata and controls
469 lines (392 loc) · 14.2 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
#include <bits/stdc++.h>
using namespace std;
/*
================================================================================
MONOTONIC STACK CONCEPTS
================================================================================
A Monotonic Stack is a stack that maintains elements in either:
- Strictly Increasing order (from bottom to top)
- Strictly Decreasing order (from bottom to top) -> pop smaller/equal elements
monotonic decreasing stack
================================================================================
1. MONOTONIC DECREASING STACK (Elements decrease from bottom to top)
================================================================================
Stack maintains: bottom [...larger...smaller...] top
USE CASES:
- Next Greater Element (to the right)
- Previous Greater Element (to the left)
- Find how many elements to the right are smaller
- Maximum rectangle in histogram (combined with increasing)
- Stock Span Problem
TEMPLATE:
stack<int> stk; // stores indices
for (int i = 0; i < n; i++) {
while (!stk.empty() && arr[stk.top()] <= arr[i]) {
stk.pop(); // pop smaller or equal elements
}
// stk.top() is the index of previous greater element (if exists)
stk.push(i);
}
PROBLEMS SOLVED:
1. Next Greater Element I & II (LeetCode 496, 503)
2. Daily Temperatures (LeetCode 739)
3. Stock Span Problem (LeetCode 901)
4. Online Stock Span
5. Number of Visible People in a Queue (LeetCode 1944)
================================================================================
2. MONOTONIC INCREASING STACK (Elements increase from bottom to top)
================================================================================
Stack maintains: bottom [...smaller...larger...] top
USE CASES:
- Next Smaller Element (to the right)
- Previous Smaller Element (to the left)
- Find how many elements to the right are greater
- Largest Rectangle in Histogram
- Trapping Rain Water
- Remove K Digits
TEMPLATE:
stack<int> stk; // stores indices
for (int i = 0; i < n; i++) {
while (!stk.empty() && arr[stk.top()] >= arr[i]) {
stk.pop(); // pop larger or equal elements
}
// stk.top() is the index of previous smaller element (if exists)
stk.push(i);
}
PROBLEMS SOLVED:
1. Next Smaller Element
2. Largest Rectangle in Histogram (LeetCode 84)
3. Maximal Rectangle (LeetCode 85)
4. Trapping Rain Water (LeetCode 42)
5. Remove K Digits (LeetCode 402)
6. Sum of Subarray Minimums (LeetCode 907)
7. Sum of Subarray Ranges (LeetCode 2104)
================================================================================
*/
// ============================================================================
// MONOTONIC DECREASING STACK EXAMPLES
// ============================================================================
/*
PROBLEM 1: Next Greater Element (Circular Array)
------------------------------------------------
Input: nums = [1,2,3,4,3]
Output: [2,3,4,-1,4]
For circular: nums = [1,2,1]
Output: [2,-1,2]
*/
vector<int> nextGreaterElementCircular(vector<int>& nums) {
int n = nums.size();
stack<int> stk; // monotonic decreasing (stores indices)
vector<int> ans(n, -1);
// First pass: push all indices (simulates circular nature)
for (int i = n - 1; i >= 0; i--)
stk.push(i);
// Second pass: find next greater for each element
for (int i = n - 1; i >= 0; i--) {
// Pop elements that are smaller or equal (maintain decreasing order)
while (!stk.empty() && nums[stk.top()] <= nums[i]) {
stk.pop();
}
if (!stk.empty()) {
ans[i] = nums[stk.top()];
}
stk.push(i);
}
return ans;
}
/*
PROBLEM 2: Next Greater Element (Non-circular, Left to Right approach)
----------------------------------------------------------------------
Input: nums = [2,1,2,4,3]
Output: [4,2,4,-1,-1]
*/
vector<int> nextGreaterElement(vector<int>& nums) {
int n = nums.size();
stack<int> stk; // monotonic decreasing (stores indices)
vector<int> ans(n, -1);
for (int i = 0; i < n; i++) {
// For all elements in stack that are smaller than current
// current element is their next greater element
while (!stk.empty() && nums[stk.top()] < nums[i]) {
ans[stk.top()] = nums[i];
stk.pop();
}
stk.push(i);
}
return ans;
}
/*
PROBLEM 3: Daily Temperatures (LeetCode 739)
--------------------------------------------
Given temperatures, return array where ans[i] = number of days
until a warmer temperature.
Input: temperatures = [73,74,75,71,69,72,76,73]
Output: [1,1,4,2,1,1,0,0]
*/
vector<int> dailyTemperatures(vector<int>& temperatures) {
int n = temperatures.size();
stack<int> stk; // monotonic decreasing
vector<int> ans(n, 0);
for (int i = 0; i < n; i++) {
while (!stk.empty() && temperatures[stk.top()] < temperatures[i]) {
int idx = stk.top();
stk.pop();
ans[idx] = i - idx; // number of days to wait
}
stk.push(i);
}
return ans;
}
/*
PROBLEM 4: Stock Span Problem (LeetCode 901)
--------------------------------------------
Span = number of consecutive days before (including today)
where price was <= today's price.
Input: prices = [100, 80, 60, 70, 60, 75, 85]
Output: [1, 1, 1, 2, 1, 4, 6]
*/
vector<int> stockSpan(vector<int>& prices) {
int n = prices.size();
stack<int> stk; // monotonic decreasing (stores indices)
vector<int> span(n);
for (int i = 0; i < n; i++) {
// Pop all prices smaller than or equal to current
while (!stk.empty() && prices[stk.top()] <= prices[i]) {
stk.pop();
}
// Span = distance from previous greater element (or start)
span[i] = stk.empty() ? (i + 1) : (i - stk.top());
stk.push(i);
}
return span;
}
// ============================================================================
// MONOTONIC INCREASING STACK EXAMPLES
// ============================================================================
/*
PROBLEM 5: Next Smaller Element
-------------------------------
Input: nums = [4, 8, 5, 2, 25]
Output: [2, 5, 2, -1, -1]
*/
vector<int> nextSmallerElement(vector<int>& nums) {
int n = nums.size();
stack<int> stk; // monotonic increasing (stores indices)
vector<int> ans(n, -1);
for (int i = 0; i < n; i++) {
// For all elements in stack that are greater than current
// current element is their next smaller element
while (!stk.empty() && nums[stk.top()] > nums[i]) {
ans[stk.top()] = nums[i];
stk.pop();
}
stk.push(i);
}
return ans;
}
/*
PROBLEM 6: Previous Smaller Element
-----------------------------------
Input: nums = [4, 5, 2, 10, 8]
Output: [-1, 4, -1, 2, 2]
*/
vector<int> previousSmallerElement(vector<int>& nums) {
int n = nums.size();
stack<int> stk; // monotonic increasing
vector<int> ans(n, -1);
for (int i = 0; i < n; i++) {
// Pop all elements >= current (maintain increasing order)
while (!stk.empty() && nums[stk.top()] >= nums[i]) {
stk.pop();
}
if (!stk.empty()) {
ans[i] = nums[stk.top()];
}
stk.push(i);
}
return ans;
}
/*
PROBLEM 7: Largest Rectangle in Histogram (LeetCode 84)
-------------------------------------------------------
Find the largest rectangular area in a histogram.
Input: heights = [2,1,5,6,2,3]
Output: 10 (rectangle with height 5, width 2)
Approach: For each bar, find:
- Previous smaller element (left boundary)
- Next smaller element (right boundary)
- Width = right - left - 1
- Area = height * width
*/
int largestRectangleArea(vector<int>& heights) {
int n = heights.size();
stack<int> stk; // monotonic increasing
int maxArea = 0;
for (int i = 0; i <= n; i++) {
int currHeight = (i == n) ? 0 : heights[i]; // to process remaining bars in the stack
// Pop elements taller than current (they can't extend further right)
while (!stk.empty() && heights[stk.top()] > currHeight) {
int height = heights[stk.top()];
stk.pop();
int width = stk.empty() ? i : (i - stk.top() - 1);
maxArea = max(maxArea, height * width);
}
stk.push(i);
}
return maxArea;
}
/*
PROBLEM 8: Trapping Rain Water (LeetCode 42)
--------------------------------------------
Given elevation map, compute trapped water after rain.
Input: height = [0,1,0,2,1,0,1,3,2,1,2,1]
Output: 6
Stack approach: Find water trapped between bars.
*/
int trap(vector<int>& height) {
int n = height.size();
stack<int> stk; // monotonic increasing (sort of)
int water = 0;
for (int i = 0; i < n; i++) {
while (!stk.empty() && height[stk.top()] < height[i]) {
int bottom = stk.top();
stk.pop();
if (stk.empty()) break;
int width = i - stk.top() - 1;
int boundedHeight = min(height[i], height[stk.top()]) - height[bottom];
water += width * boundedHeight;
}
stk.push(i);
}
return water;
}
/*
PROBLEM 9: Sum of Subarray Minimums (LeetCode 907)
--------------------------------------------------
Return sum of min(subarray) for all subarrays, mod 10^9+7.
Input: arr = [3,1,2,4]
Output: 17
Subarrays: [3], [1], [2], [4], [3,1], [1,2], [2,4], [3,1,2], [1,2,4], [3,1,2,4]
Minimums: 3 1 2 4 1 1 2 1 1 1
Sum = 17
Key insight: For each element, count how many subarrays have it as minimum.
- Find previous smaller element (left boundary)
- Find next smaller element (right boundary)
- Count = (i - left) * (right - i)
*/
int sumSubarrayMins(vector<int>& arr) {
const int MOD = 1e9 + 7;
int n = arr.size();
vector<int> left(n), right(n);
stack<int> stk;
// Previous smaller element
for (int i = 0; i < n; i++) {
while (!stk.empty() && arr[stk.top()] >= arr[i]) {
stk.pop();
}
left[i] = stk.empty() ? -1 : stk.top();
stk.push(i);
}
while (!stk.empty()) stk.pop();
// Next smaller element
for (int i = n - 1; i >= 0; i--) {
while (!stk.empty() && arr[stk.top()] > arr[i]) {
stk.pop();
}
right[i] = stk.empty() ? n : stk.top();
stk.push(i);
}
// Calculate contribution of each element
long long ans = 0;
for (int i = 0; i < n; i++) {
long long leftCount = i - left[i];
long long rightCount = right[i] - i;
ans = (ans + (long long)arr[i] * leftCount % MOD * rightCount % MOD) % MOD;
}
return ans;
}
/*
PROBLEM 10: Remove K Digits (LeetCode 402)
------------------------------------------
Remove k digits to make the smallest possible number.
Input: num = "1432219", k = 3
Output: "1219"
Remove: 4, 3, 2 -> "1219"
Approach: Use monotonic increasing stack.
Remove larger digits when a smaller digit comes.
*/
string removeKdigits(string num, int k) {
stack<char> stk; // monotonic increasing
for (char digit : num) {
// Remove larger digits from stack if we can
while (!stk.empty() && k > 0 && stk.top() > digit) {
stk.pop();
k--;
}
stk.push(digit);
}
// Remove remaining k digits from end
while (k > 0 && !stk.empty()) {
stk.pop();
k--;
}
// Build result
string result = "";
while (!stk.empty()) {
result += stk.top();
stk.pop();
}
reverse(result.begin(), result.end());
// Remove leading zeros
int start = 0;
while (start < result.size() && result[start] == '0') {
start++;
}
result = result.substr(start);
return result.empty() ? "0" : result;
}
// ============================================================================
// SUMMARY TABLE
// ============================================================================
/*
+---------------------------+------------------------+------------------------+
| Problem Pattern | Stack Type | What Stack Stores |
+---------------------------+------------------------+------------------------+
| Next Greater Element | Monotonic Decreasing | Indices of elements |
| Previous Greater Element | Monotonic Decreasing | Indices of elements |
| Daily Temperatures | Monotonic Decreasing | Indices of days |
| Stock Span | Monotonic Decreasing | Indices of prices |
+---------------------------+------------------------+------------------------+
| Next Smaller Element | Monotonic Increasing | Indices of elements |
| Previous Smaller Element | Monotonic Increasing | Indices of elements |
| Largest Rectangle Hist | Monotonic Increasing | Indices of heights |
| Trapping Rain Water | Monotonic Increasing | Indices of heights |
| Sum of Subarray Minimums | Monotonic Increasing | Indices of elements |
| Remove K Digits | Monotonic Increasing | Digit characters |
+---------------------------+------------------------+------------------------+
KEY INSIGHTS:
1. Decreasing Stack -> Looking for GREATER elements
2. Increasing Stack -> Looking for SMALLER elements
3. Usually store INDICES (not values) to calculate distances
4. Iterate left-to-right OR right-to-left based on "next" vs "previous"
5. Time Complexity: O(n) - each element pushed and popped at most once
6. Space Complexity: O(n) - stack size
*/
int main() {
// Example: Next Greater Element (Circular)
vector<int> nums1 = {1, 2, 3, 4, 3};
vector<int> result1 = nextGreaterElementCircular(nums1);
// Expected: [2, 3, 4, -1, 4]
// Example: Daily Temperatures
vector<int> temps = {73, 74, 75, 71, 69, 72, 76, 73};
vector<int> result2 = dailyTemperatures(temps);
// Expected: [1, 1, 4, 2, 1, 1, 0, 0]
// Example: Largest Rectangle in Histogram
vector<int> heights = {2, 1, 5, 6, 2, 3};
int maxArea = largestRectangleArea(heights);
// Expected: 10
// Example: Remove K Digits
string smallest = removeKdigits("1432219", 3);
// Expected: "1219"
return 0;
}