-
Notifications
You must be signed in to change notification settings - Fork 4
Expand file tree
/
Copy path01_BasicProblems.cpp
More file actions
559 lines (460 loc) · 14.1 KB
/
Copy path01_BasicProblems.cpp
File metadata and controls
559 lines (460 loc) · 14.1 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
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
/*
================================================================================
STACK - BASIC PROBLEMS
================================================================================
Pattern 1: Matching / Validation
Pattern 2: Design Problems (Min Stack, etc.)
Pattern 3: String Processing
Pattern 4: Simulation Problems
================================================================================
*/
#include <iostream>
#include <stack>
#include <vector>
#include <string>
#include <unordered_map>
#include <queue>
using namespace std;
/*
================================================================================
PATTERN 1: MATCHING / VALIDATION
================================================================================
*/
// 1.1 VALID PARENTHESES
// LeetCode: 20. Valid Parentheses
// Time: O(n), Space: O(n)
bool isValid(string s) {
stack<char> stk;
unordered_map<char, char> pairs = {
{')', '('},
{']', '['},
{'}', '{'}
};
for (char c : s) {
if (c == '(' || c == '[' || c == '{') {
stk.push(c);
} else {
if (stk.empty() || stk.top() != pairs[c]) {
return false;
}
stk.pop();
}
}
return stk.empty();
}
// "()" -> true, "()[]{}" -> true, "(]" -> false
// 1.2 MINIMUM ADD TO MAKE PARENTHESES VALID
// LeetCode: 921. Minimum Add to Make Parentheses Valid
int minAddToMakeValid(string s) {
int open = 0; // Unmatched '('
int close = 0; // Unmatched ')'
for (char c : s) {
if (c == '(') {
open++;
} else {
if (open > 0) {
open--; // Match with open
} else {
close++; // Unmatched close
}
}
}
return open + close;
}
// "())" -> 1, "(((" -> 3, "()" -> 0
// 1.3 REMOVE INVALID PARENTHESES (Simplified - remove minimum)
// LeetCode: 1249. Minimum Remove to Make Valid Parentheses
string minRemoveToMakeValid(string s) {
stack<int> stk; // Indices of '('
vector<bool> toRemove(s.size(), false);
for (int i = 0; i < s.size(); i++) {
if (s[i] == '(') {
stk.push(i);
} else if (s[i] == ')') {
if (stk.empty()) {
toRemove[i] = true; // Unmatched ')'
} else {
stk.pop();
}
}
}
// Mark unmatched '('
while (!stk.empty()) {
toRemove[stk.top()] = true;
stk.pop();
}
string result;
for (int i = 0; i < s.size(); i++) {
if (!toRemove[i]) {
result += s[i];
}
}
return result;
}
// 1.4 SCORE OF PARENTHESES
// LeetCode: 856. Score of Parentheses
// () = 1, (A) = 2*A, AB = A + B
int scoreOfParentheses(string s) {
stack<int> stk;
stk.push(0); // Current score at this level
for (char c : s) {
if (c == '(') {
stk.push(0); // New level
} else {
int inner = stk.top(); stk.pop();
int outer = stk.top(); stk.pop();
// () = 1, or (A) = 2*A
stk.push(outer + max(2 * inner, 1));
}
}
return stk.top();
}
// "()" -> 1, "(())" -> 2, "()()" -> 2, "(()(()))" -> 6
/*
================================================================================
PATTERN 2: DESIGN PROBLEMS
================================================================================
*/
// 2.1 MIN STACK
// LeetCode: 155. Min Stack
// All operations in O(1)
class MinStack {
private:
stack<int> stk;
stack<int> minStk; // Track minimums
public:
MinStack() {}
void push(int val) {
stk.push(val);
if (minStk.empty() || val <= minStk.top()) {
minStk.push(val);
}
}
void pop() {
if (stk.top() == minStk.top()) {
minStk.pop();
}
stk.pop();
}
int top() {
return stk.top();
}
int getMin() {
return minStk.top();
}
};
// Alternative: Store pair of (value, min_at_this_point)
class MinStack2 {
private:
stack<pair<int, int>> stk; // {value, current_min}
public:
void push(int val) {
int newMin = stk.empty() ? val : min(val, stk.top().second);
stk.push({val, newMin});
}
void pop() { stk.pop(); }
int top() { return stk.top().first; }
int getMin() { return stk.top().second; }
};
// 2.2 MAX STACK
// LeetCode: 716. Max Stack (Premium)
class MaxStack {
private:
stack<int> stk;
stack<int> maxStk;
public:
void push(int val) {
stk.push(val);
int newMax = maxStk.empty() ? val : max(val, maxStk.top());
maxStk.push(newMax);
}
int pop() {
int val = stk.top();
stk.pop();
maxStk.pop();
return val;
}
int top() { return stk.top(); }
int peekMax() { return maxStk.top(); }
// popMax() is more complex - requires different approach
};
// 2.3 IMPLEMENT QUEUE USING STACKS
// LeetCode: 232. Implement Queue using Stacks
class MyQueue {
private:
stack<int> input; // For push
stack<int> output; // For pop/peek
void transfer() {
if (output.empty()) {
while (!input.empty()) {
output.push(input.top());
input.pop();
}
}
}
public:
void push(int x) {
input.push(x);
}
int pop() {
transfer();
int val = output.top();
output.pop();
return val;
}
int peek() {
transfer();
return output.top();
}
bool empty() {
return input.empty() && output.empty();
}
};
// Amortized O(1) for all operations
// 2.4 IMPLEMENT STACK USING QUEUES
// LeetCode: 225. Implement Stack using Queues
class MyStack {
private:
queue<int> q;
public:
void push(int x) {
q.push(x);
// Rotate to make new element at front
for (int i = 0; i < q.size() - 1; i++) {
q.push(q.front());
q.pop();
}
}
int pop() {
int val = q.front();
q.pop();
return val;
}
int top() {
return q.front();
}
bool empty() {
return q.empty();
}
};
// Push: O(n), Pop/Top: O(1)
/*
================================================================================
PATTERN 3: STRING PROCESSING
================================================================================
*/
// 3.1 DECODE STRING
// LeetCode: 394. Decode String
// "3[a2[c]]" -> "accaccacc"
string decodeString(string s) {
stack<int> countStack;
stack<string> stringStack;
string current = "";
int k = 0;
for (char c : s) {
if (isdigit(c)) {
k = k * 10 + (c - '0');
} else if (c == '[') {
countStack.push(k);
stringStack.push(current);
current = "";
k = 0;
} else if (c == ']') {
string temp = current;
current = stringStack.top(); stringStack.pop();
int count = countStack.top(); countStack.pop();
for (int i = 0; i < count; i++) {
current += temp;
}
} else {
current += c;
}
}
return current;
}
// 3.2 REMOVE ALL ADJACENT DUPLICATES
// LeetCode: 1047. Remove All Adjacent Duplicates In String
string removeDuplicates(string s) {
string result; // Use string as stack
for (char c : s) {
if (!result.empty() && result.back() == c) {
result.pop_back();
} else {
result.push_back(c);
}
}
return result;
}
// "abbaca" -> "ca"
// 3.3 REMOVE ALL ADJACENT DUPLICATES II
// LeetCode: 1209. Remove All Adjacent Duplicates in String II
string removeDuplicates(string s, int k) {
stack<pair<char, int>> stk; // {char, count}
for (char c : s) {
if (!stk.empty() && stk.top().first == c) {
stk.top().second++;
if (stk.top().second == k) {
stk.pop();
}
} else {
stk.push({c, 1});
}
}
string result;
while (!stk.empty()) {
result = string(stk.top().second, stk.top().first) + result;
stk.pop();
}
return result;
}
// "deeedbbcccbdaa", k=3 -> "aa"
// 3.4 SIMPLIFY PATH
// LeetCode: 71. Simplify Path
string simplifyPath(string path) {
stack<string> stk;
stringstream ss(path);
string token;
while (getline(ss, token, '/')) {
if (token == "" || token == ".") {
continue; // Ignore
} else if (token == "..") {
if (!stk.empty()) stk.pop();
} else {
stk.push(token);
}
}
string result;
while (!stk.empty()) {
result = "/" + stk.top() + result;
stk.pop();
}
return result.empty() ? "/" : result;
}
// "/a/./b/../../c/" -> "/c"
/*
================================================================================
PATTERN 4: SIMULATION PROBLEMS
================================================================================
*/
// 4.1 BASEBALL GAME
// LeetCode: 682. Baseball Game
int calPoints(vector<string>& operations) {
stack<int> stk;
for (const string& op : operations) {
if (op == "+") {
int top = stk.top(); stk.pop();
int newTop = top + stk.top();
stk.push(top);
stk.push(newTop);
} else if (op == "D") {
stk.push(2 * stk.top());
} else if (op == "C") {
stk.pop();
} else {
stk.push(stoi(op));
}
}
int sum = 0;
while (!stk.empty()) {
sum += stk.top();
stk.pop();
}
return sum;
}
// 4.2 ASTEROID COLLISION
// LeetCode: 735. Asteroid Collision
// Positive = moving right, Negative = moving left
vector<int> asteroidCollision(vector<int>& asteroids) {
stack<int> stk;
for (int asteroid : asteroids) {
bool destroyed = false;
// Collision happens when: stack has right-moving (+) and current is left-moving (-)
while (!stk.empty() && stk.top() > 0 && asteroid < 0) {
if (stk.top() < -asteroid) {
stk.pop(); // Stack asteroid destroyed
continue;
} else if (stk.top() == -asteroid) {
stk.pop(); // Both destroyed
}
destroyed = true;
break;
}
if (!destroyed) {
stk.push(asteroid);
}
}
vector<int> result(stk.size());
for (int i = stk.size() - 1; i >= 0; i--) {
result[i] = stk.top();
stk.pop();
}
return result;
}
// [5, 10, -5] -> [5, 10]
// [8, -8] -> []
// [10, 2, -5] -> [10]
// 4.3 BACKSPACE STRING COMPARE
// LeetCode: 844. Backspace String Compare
bool backspaceCompare(string s, string t) {
auto process = [](const string& str) {
string result;
for (char c : str) {
if (c == '#') {
if (!result.empty()) result.pop_back();
} else {
result.push_back(c);
}
}
return result;
};
return process(s) == process(t);
}
// "ab#c", "ad#c" -> true (both become "ac")
// ============== MAIN - DEMO ==============
int main() {
cout << "=== Stack Basic Problems ===\n\n";
// Valid Parentheses
cout << "--- Valid Parentheses ---" << endl;
cout << "\"()[]{}\" is valid: " << (isValid("()[]{}") ? "Yes" : "No") << endl;
cout << "\"(]\" is valid: " << (isValid("(]") ? "Yes" : "No") << endl;
// Min Stack
cout << "\n--- Min Stack ---" << endl;
MinStack minStk;
minStk.push(-2);
minStk.push(0);
minStk.push(-3);
cout << "Min: " << minStk.getMin() << endl; // -3
minStk.pop();
cout << "Top: " << minStk.top() << endl; // 0
cout << "Min: " << minStk.getMin() << endl; // -2
// Decode String
cout << "\n--- Decode String ---" << endl;
cout << "\"3[a2[c]]\" -> " << decodeString("3[a2[c]]") << endl;
// Remove Duplicates
cout << "\n--- Remove Duplicates ---" << endl;
cout << "\"abbaca\" -> " << removeDuplicates("abbaca") << endl;
// Score of Parentheses
cout << "\n--- Score of Parentheses ---" << endl;
cout << "\"(()(()))\" -> " << scoreOfParentheses("(()(()))") << endl;
// Simplify Path
cout << "\n--- Simplify Path ---" << endl;
cout << "\"/a/./b/../../c/\" -> " << simplifyPath("/a/./b/../../c/") << endl;
return 0;
}
/*
================================================================================
SUMMARY
================================================================================
┌───────────────────────────────┬─────────────────────────────────────────────┐
│ Pattern │ Key Technique │
├───────────────────────────────┼─────────────────────────────────────────────┤
│ Parentheses Matching │ Push open, pop on close, check match │
│ Min/Max Stack │ Secondary stack for extremes │
│ Queue using Stacks │ Two stacks: input + output │
│ Decode String │ Stack for counts & strings at each level │
│ Remove Duplicates │ Compare with stack top │
│ Asteroid Collision │ Process collisions with stack top │
└───────────────────────────────┴─────────────────────────────────────────────┘
TIP: Many string problems can use the string itself as a stack (push_back/pop_back)!
================================================================================
*/