-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathstack.py
More file actions
546 lines (444 loc) · 15.5 KB
/
stack.py
File metadata and controls
546 lines (444 loc) · 15.5 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
"""
Stack Algorithm
==============
Used for: Parsing, matching brackets, monotonic stack problems, expression evaluation
Time Complexity: O(n) for most operations
Space Complexity: O(n) for stack storage
"""
from typing import List, Optional, Dict, Tuple
from collections import deque
# ==================== BRUTE FORCE APPROACHES ====================
def valid_parentheses_brute_force(s: str) -> bool:
"""
Brute Force Parentheses: Check all combinations by counting
Time Complexity: O(n²) - nested loops for different bracket types
Space Complexity: O(1)
Problems:
- Doesn't handle nested structures properly
- Can't distinguish between different bracket types correctly
- Inefficient for complex expressions
"""
# Count each type of bracket
round_count = square_count = curly_count = 0
for char in s:
if char == '(':
round_count += 1
elif char == ')':
round_count -= 1
elif char == '[':
square_count += 1
elif char == ']':
square_count -= 1
elif char == '{':
curly_count += 1
elif char == '}':
curly_count -= 1
# This approach fails for cases like "([)]"
if round_count < 0 or square_count < 0 or curly_count < 0:
return False
return round_count == 0 and square_count == 0 and curly_count == 0
def evaluate_expression_brute_force(expression: str) -> int:
"""
Brute Force Expression Evaluation: Use built-in eval
Time Complexity: O(n)
Space Complexity: O(n)
Problems:
- Security risk with eval()
- No control over evaluation process
- Doesn't demonstrate algorithmic approach
"""
try:
# This is unsafe and not algorithmic
return eval(expression)
except:
return 0
# ==================== OPTIMIZED STACK APPROACHES ====================
def valid_parentheses_stack(s: str) -> bool:
"""
Valid Parentheses using Stack
Time Complexity: O(n)
Space Complexity: O(n) - worst case all opening brackets
Advantages:
- Correctly handles nested structures
- Distinguishes between bracket types
- Linear time complexity
"""
stack = []
mapping = {')': '(', '}': '{', ']': '['}
for char in s:
if char in mapping: # Closing bracket
if not stack or stack.pop() != mapping[char]:
return False
else: # Opening bracket
stack.append(char)
return len(stack) == 0
def evaluate_postfix_expression(tokens: List[str]) -> int:
"""
Evaluate Reverse Polish Notation (Postfix) using Stack
Time Complexity: O(n)
Space Complexity: O(n)
"""
stack = []
for token in tokens:
if token in ['+', '-', '*', '/']:
# Pop two operands
b = stack.pop()
a = stack.pop()
if token == '+':
result = a + b
elif token == '-':
result = a - b
elif token == '*':
result = a * b
elif token == '/':
# Handle division (truncate towards zero)
result = int(a / b)
stack.append(result)
else:
# Push operand
stack.append(int(token))
return stack[0]
def infix_to_postfix(expression: str) -> str:
"""
Convert infix expression to postfix using stack
Time Complexity: O(n)
Space Complexity: O(n)
"""
precedence = {'+': 1, '-': 1, '*': 2, '/': 2, '^': 3}
right_associative = {'^'}
stack = []
output = []
for char in expression:
if char.isalnum(): # Operand
output.append(char)
elif char == '(':
stack.append(char)
elif char == ')':
while stack and stack[-1] != '(':
output.append(stack.pop())
stack.pop() # Remove '('
elif char in precedence: # Operator
while (stack and stack[-1] != '(' and
stack[-1] in precedence and
(precedence[stack[-1]] > precedence[char] or
(precedence[stack[-1]] == precedence[char] and
char not in right_associative))):
output.append(stack.pop())
stack.append(char)
while stack:
output.append(stack.pop())
return ''.join(output)
def basic_calculator(s: str) -> int:
"""
Basic Calculator with +, -, (, ) using Stack
Time Complexity: O(n)
Space Complexity: O(n)
"""
stack = []
result = 0
number = 0
sign = 1
for char in s:
if char.isdigit():
number = number * 10 + int(char)
elif char == '+':
result += sign * number
number = 0
sign = 1
elif char == '-':
result += sign * number
number = 0
sign = -1
elif char == '(':
# Push current result and sign onto stack
stack.append(result)
stack.append(sign)
# Reset for expression inside parentheses
result = 0
sign = 1
elif char == ')':
result += sign * number
number = 0
# Pop sign and previous result
result *= stack.pop() # sign
result += stack.pop() # previous result
return result + sign * number
def daily_temperatures(temperatures: List[int]) -> List[int]:
"""
Daily Temperatures using Monotonic Stack
Time Complexity: O(n) - each element pushed/popped once
Space Complexity: O(n) - stack size
Find next warmer temperature for each day
"""
result = [0] * len(temperatures)
stack = [] # Monotonic decreasing stack (indices)
for i, temp in enumerate(temperatures):
# Pop all temperatures that are cooler than current
while stack and temperatures[stack[-1]] < temp:
prev_index = stack.pop()
result[prev_index] = i - prev_index
stack.append(i)
return result
def next_greater_element(nums1: List[int], nums2: List[int]) -> List[int]:
"""
Next Greater Element using Stack
Time Complexity: O(n + m)
Space Complexity: O(n)
"""
# Build next greater element map for nums2
next_greater = {}
stack = []
for num in nums2:
while stack and stack[-1] < num:
next_greater[stack.pop()] = num
stack.append(num)
# For remaining elements in stack, no greater element exists
while stack:
next_greater[stack.pop()] = -1
# Build result for nums1
return [next_greater[num] for num in nums1]
def largest_rectangle_histogram(heights: List[int]) -> int:
"""
Largest Rectangle in Histogram using Stack
Time Complexity: O(n)
Space Complexity: O(n)
"""
stack = [] # Stack of indices
max_area = 0
heights.append(0) # Add sentinel to process remaining bars
for i, height in enumerate(heights):
while stack and heights[stack[-1]] > height:
h = heights[stack.pop()]
w = i if not stack else i - stack[-1] - 1
max_area = max(max_area, h * w)
stack.append(i)
heights.pop() # Remove sentinel
return max_area
def trapping_rain_water_stack(heights: List[int]) -> int:
"""
Trapping Rain Water using Stack
Time Complexity: O(n)
Space Complexity: O(n)
"""
stack = []
water_trapped = 0
for i, height in enumerate(heights):
while stack and heights[stack[-1]] < height:
bottom = stack.pop()
if not stack:
break
distance = i - stack[-1] - 1
bounded_height = min(height, heights[stack[-1]]) - heights[bottom]
water_trapped += distance * bounded_height
stack.append(i)
return water_trapped
def remove_duplicate_letters(s: str) -> str:
"""
Remove Duplicate Letters using Monotonic Stack
Time Complexity: O(n)
Space Complexity: O(1) - limited by alphabet size
"""
# Count frequency of each character
count = {}
for char in s:
count[char] = count.get(char, 0) + 1
stack = []
in_stack = set()
for char in s:
count[char] -= 1
if char in in_stack:
continue
# Remove characters that are lexicographically larger
# and appear later in the string
while (stack and stack[-1] > char and
count[stack[-1]] > 0):
removed = stack.pop()
in_stack.remove(removed)
stack.append(char)
in_stack.add(char)
return ''.join(stack)
def decode_string(s: str) -> str:
"""
Decode String using Stack
Time Complexity: O(n * max_k) where max_k is maximum multiplier
Space Complexity: O(n)
"""
stack = []
current_num = 0
current_string = ""
for char in s:
if char.isdigit():
current_num = current_num * 10 + int(char)
elif char == '[':
# Push current state to stack
stack.append((current_string, current_num))
current_string = ""
current_num = 0
elif char == ']':
# Pop from stack and decode
prev_string, num = stack.pop()
current_string = prev_string + current_string * num
else:
current_string += char
return current_string
def min_stack_design():
"""
Design Min Stack that supports getMin() in O(1)
"""
class MinStack:
def __init__(self):
self.stack = []
self.min_stack = []
def push(self, val: int) -> None:
self.stack.append(val)
# Push to min_stack if it's empty or val is <= current min
if not self.min_stack or val <= self.min_stack[-1]:
self.min_stack.append(val)
def pop(self) -> None:
if self.stack:
val = self.stack.pop()
# Pop from min_stack if it's the minimum
if self.min_stack and val == self.min_stack[-1]:
self.min_stack.pop()
def top(self) -> int:
return self.stack[-1] if self.stack else None
def getMin(self) -> int:
return self.min_stack[-1] if self.min_stack else None
return MinStack()
def simplify_path(path: str) -> str:
"""
Simplify Unix-style path using Stack
Time Complexity: O(n)
Space Complexity: O(n)
"""
stack = []
components = path.split('/')
for component in components:
if component == '' or component == '.':
continue
elif component == '..':
if stack:
stack.pop()
else:
stack.append(component)
return '/' + '/'.join(stack)
# ==================== EXAMPLE USAGE ====================
if __name__ == "__main__":
print("=== Valid Parentheses ===")
test_cases = ["()", "()[]{}", "(]", "([)]", "{[]}"]
for test in test_cases:
brute_result = valid_parentheses_brute_force(test)
stack_result = valid_parentheses_stack(test)
print(f"'{test}': brute={brute_result}, stack={stack_result}")
print("\n=== Postfix Expression Evaluation ===")
postfix = ["2", "1", "+", "3", "*"]
result = evaluate_postfix_expression(postfix)
print(f"Postfix {postfix} = {result}")
print("\n=== Infix to Postfix Conversion ===")
infix = "a+b*c"
postfix_result = infix_to_postfix(infix)
print(f"Infix '{infix}' -> Postfix '{postfix_result}'")
print("\n=== Basic Calculator ===")
expressions = ["1 + 1", "2-1 + 2", "(1+(4+5+2)-3)+(6+8)"]
for expr in expressions:
result = basic_calculator(expr)
print(f"'{expr}' = {result}")
print("\n=== Daily Temperatures ===")
temperatures = [73, 74, 75, 71, 69, 72, 76, 73]
result = daily_temperatures(temperatures)
print(f"Temperatures: {temperatures}")
print(f"Days to wait: {result}")
print("\n=== Next Greater Element ===")
nums1, nums2 = [4, 1, 2], [1, 3, 4, 2]
result = next_greater_element(nums1, nums2)
print(f"nums1: {nums1}, nums2: {nums2}")
print(f"Next greater: {result}")
print("\n=== Largest Rectangle in Histogram ===")
heights = [2, 1, 5, 6, 2, 3]
max_area = largest_rectangle_histogram(heights)
print(f"Heights: {heights}")
print(f"Largest rectangle area: {max_area}")
print("\n=== Trapping Rain Water ===")
heights = [0, 1, 0, 2, 1, 0, 1, 3, 2, 1, 2, 1]
water = trapping_rain_water_stack(heights)
print(f"Heights: {heights}")
print(f"Water trapped: {water}")
print("\n=== Remove Duplicate Letters ===")
s = "bcabc"
result = remove_duplicate_letters(s)
print(f"String: '{s}' -> '{result}'")
print("\n=== Decode String ===")
s = "3[a2[c]]"
result = decode_string(s)
print(f"Encoded: '{s}' -> Decoded: '{result}'")
print("\n=== Min Stack Demo ===")
min_stack = min_stack_design()
operations = [(-2, 'push'), (0, 'push'), (-3, 'push'),
(None, 'getMin'), (None, 'pop'), (None, 'top'), (None, 'getMin')]
for val, op in operations:
if op == 'push':
min_stack.push(val)
print(f"Push {val}")
elif op == 'pop':
min_stack.pop()
print("Pop")
elif op == 'top':
print(f"Top: {min_stack.top()}")
elif op == 'getMin':
print(f"Min: {min_stack.getMin()}")
print("\n=== Simplify Path ===")
paths = ["/home/", "/../", "/home//foo/", "/a/./b/../../c/"]
for path in paths:
simplified = simplify_path(path)
print(f"'{path}' -> '{simplified}'")
"""
STACK PATTERNS:
1. Matching/Parsing Problems:
- Parentheses validation
- Expression evaluation
- HTML/XML parsing
- Template pattern matching
2. Monotonic Stack:
- Next/Previous greater/smaller element
- Daily temperatures, stock spans
- Largest rectangle problems
- Remove duplicates maintaining order
3. Expression Evaluation:
- Infix to postfix conversion
- Calculator implementations
- Operator precedence handling
4. Backtracking Support:
- DFS traversal
- Undo operations
- State management
WHEN TO USE STACK:
- Need LIFO (Last In, First Out) behavior
- Matching/balancing problems
- Expression parsing and evaluation
- Maintaining monotonic properties
- Recursive algorithms (implicit stack)
STACK vs OTHER DATA STRUCTURES:
- vs Queue: LIFO vs FIFO behavior
- vs Array: Dynamic size, O(1) top access
- vs Linked List: Restricted access pattern
MONOTONIC STACK TECHNIQUE:
- Maintain elements in increasing/decreasing order
- Pop elements that violate monotonic property
- Useful for "next greater/smaller" problems
- Each element pushed/popped at most once
COMMON STACK OPERATIONS:
- push(x): Add element to top - O(1)
- pop(): Remove top element - O(1)
- top()/peek(): View top element - O(1)
- isEmpty(): Check if empty - O(1)
- size(): Get number of elements - O(1)
IMPLEMENTATION CONSIDERATIONS:
- Array-based: Fixed size, cache-friendly
- Linked List-based: Dynamic size, extra memory overhead
- Language built-ins: Usually optimized implementations
ADVANCED PATTERNS:
- Two stacks for queue implementation
- Stack for recursive to iterative conversion
- Min/Max stack maintaining auxiliary information
- Stack-based tree traversal algorithms
"""