Stack
Phase 2 14 solutionsStack
Problem List
| # | Problem | Difficulty | Category |
|---|---|---|---|
| 1 | Baseball Game (LC 682) | Easy | Simulation |
| 2 | Valid Parentheses (LC 20) | Easy | Matching |
| 3 | Implement Stack Using Queues (LC 225) | Easy | Design |
| 4 | Implement Queue using Stacks (LC 232) | Easy | Design |
| 5 | Min Stack (LC 155) | Medium | Design |
| 6 | Evaluate Reverse Polish Notation (LC 150) | Medium | Simulation |
| 7 | Asteroid Collision (LC 735) | Medium | Simulation |
| 8 | Daily Temperatures (LC 739) | Medium | Monotonic |
| 9 | Online Stock Span (LC 901) | Medium | Monotonic |
| 10 | Car Fleet (LC 853) | Medium | Monotonic |
| 11 | Simplify Path (LC 71) | Medium | Simulation |
| 12 | Decode String (LC 394) | Medium | Simulation |
| 13 | Maximum Frequency Stack (LC 895) | Hard | Design |
| 14 | Largest Rectangle In Histogram (LC 84) | Hard | Monotonic |
Mermaid Diagrams
1. Stack Problem Categories
mindmap
root((Stack))
Matching
Valid Parentheses LC20
Monotonic
Daily Temperatures LC739
Online Stock Span LC901
Car Fleet LC853
Largest Rectangle LC84
Simulation
Baseball Game LC682
Evaluate RPN LC150
Asteroid Collision LC735
Simplify Path LC71
Decode String LC394
Design
Implement Stack LC225
Implement Queue LC232
Min Stack LC155
Max Freq Stack LC895
2. Monotonic Stack Pattern (Next Greater/Smaller)
flowchart TD
A[For each element arr-i] --> B{"Stack not empty AND condition met?"}
B -->|Next Greater: arr-i > stack.top| C[Pop top result-top = i]
B -->|Next Smaller: arr-i < stack.top| C
C --> B
B -->|Condition not met| D[Push i onto stack]
D --> E[Next element]
E --> A
D --> F{"End of array?"}
F -->|Yes, remaining in stack| G["No next greater/smaller result = -1 or 0"]
3. Parentheses Matching Flow (LC 20)
flowchart TD
A[Read char c] --> B{"c is opening bracket?"}
B -->|Yes| C[Push c onto stack]
B -->|No - closing bracket| D{"Stack empty?"}
D -->|Yes| E[Return False]
D -->|No| F{"stack.top matches c?"}
F -->|Yes| G[Pop stack]
F -->|No| E
G --> H[Next char]
C --> H
H --> I{"More chars?"}
I -->|Yes| A
I -->|No| J{"Stack empty?"}
J -->|Yes| K[Return True]
J -->|No| E
4. Stack vs Queue Decision
flowchart TD
A[Need temporary storage?] --> B{"Access order?"}
B -->|Last In First Out Undo, backtrack, nested| C[Stack]
B -->|First In First Out BFS, scheduling, order| D[Queue]
C --> C1[Monotonic problems Matching brackets Expression eval]
D --> D1[BFS traversal Sliding window Task scheduling]
C --> C2{"Need O-1 min/max?"}
C2 -->|Yes| C3[Auxiliary stack Min Stack LC155]
C2 -->|No| C4[Regular stack]
Complexity Summary
| Problem | Time | Space | Key Insight |
|---|---|---|---|
| LC 682 | O(n) | O(n) | Simulate with stack |
| LC 20 | O(n) | O(n) | Push open, match close |
| LC 225 | O(n) push | O(n) | Rotate queue on push |
| LC 232 | O(1) amortized | O(n) | Two stacks: in/out |
| LC 155 | O(1) all | O(n) | Stack of (val, curMin) pairs |
| LC 150 | O(n) | O(n) | Push nums, pop on operator |
| LC 735 | O(n) | O(n) | Positive right, negative left |
| LC 739 | O(n) | O(n) | Monotonic decreasing stack |
| LC 901 | O(1) amortized | O(n) | Monotonic non-increasing |
| LC 853 | O(n log n) | O(n) | Sort by pos, stack arrival times |
| LC 71 | O(n) | O(n) | Split on /, handle .. |
| LC 394 | O(n) | O(n) | Stack of (string, count) |
| LC 895 | O(1) all | O(n) | freq map + group stacks |
| LC 84 | O(n) | O(n) | Monotonic increasing stack |
Study Order
flowchart LR
A[LC 20 Matching] --> B[LC 682 Simulation]
B --> C["LC 225 / 232 Design Basics"]
C --> D[LC 155 Min Stack]
D --> E[LC 150 RPN]
E --> F[LC 739 Monotonic]
F --> G[LC 901 Stock Span]
G --> H[LC 735 Collision]
H --> I[LC 853 Car Fleet]
I --> J[LC 394 Decode]
J --> K[LC 84 Histogram]
K --> L[LC 895 Freq Stack]
Recommended order: Matching → Simulation → Design → Monotonic → Hard
Stack — Pattern Notes
Core Intuition
A stack is the right tool when you need to defer a decision until you see a future element, or when you need to undo/match the most recent thing seen. LIFO naturally models nested structure and "nearest previous element" queries.
Sub-patterns:
-
Matching/Balancing — push open bracket; on close, pop and validate compatibility. End check: stack must be empty. Applies to valid parentheses, decode string (push entire state on
[). -
Monotonic Stack — maintain a stack whose elements stay in increasing or decreasing order by popping whenever the invariant breaks. Each pop resolves one "nearest" query in O(1) amortized.
- Use a decreasing stack (pop when current > top) to find the next greater element — daily temperatures, online stock span.
- Use an increasing stack (pop when current < top) to find the next smaller element or compute areas bounded below — largest rectangle in histogram.
- Store indices, not values, whenever you need distance or width. The value is
arr[index].
-
Simulation — stack as scratch space for stateful, ordered operations: baseball game (undo with
C), simplify path (navigate with..), asteroid collision (resolve destruction left-to-right). -
Expression Evaluation — RPN uses a single operand stack (pop two, apply operator, push result). Infix needs a separate operator stack (or convert to postfix first).
-
Design — augment a plain stack with extra state:
- Min stack: track running minimum alongside each pushed value.
- Frequency stack:
freqmap +groupmap indexed by frequency; pop always targetsmax_freqgroup. - Queue using stacks: two stacks (inbox / outbox); lazy transfer only when outbox drains.
Decision Guide
Ask these questions in order:
- Does the problem ask for the nearest greater/smaller element to the left or right? → Monotonic stack. Decreasing for next-greater, increasing for next-smaller.
- Does the problem involve matching open/close pairs or nested structure? → Push on open, pop and validate on close.
- Does the problem need to undo or revisit the last operation? → Plain stack as scratch.
- Does the problem need O(1) min/max alongside push/pop? → Augmented stack storing running extremum per frame.
- Does the problem need O(1) push/pop by frequency rank? → Frequency stack (freq map + group map).
- Does the problem need a queue but only stacks are allowed? → Two stacks, lazy transfer.
Python Skeletons
Monotonic decreasing (next greater element):
stack = [] # indices
result = [0] * n
for i in range(n):
while stack and arr[stack[-1]] < arr[i]:
idx = stack.pop()
result[idx] = i - idx # or arr[i] for value
stack.append(i)
Matching/balancing:
pairs = {')': '(', ']': '[', '}': '{'}
stack = []
for ch in s:
if ch in '([{':
stack.append(ch)
elif not stack or stack[-1] != pairs[ch]:
return False
else:
stack.pop()
return len(stack) == 0
Min stack (pair approach):
stack = [] # [(val, current_min)]
def push(val):
cur_min = min(val, stack[-1][1]) if stack else val
stack.append((val, cur_min))
def get_min():
return stack[-1][1]
Java Idioms
Never use Stack<T>. It extends Vector, is synchronized on every operation, and has the wrong performance profile. Use ArrayDeque instead.
// Stack semantics
Deque<Integer> stack = new ArrayDeque<>();
stack.push(x); // addFirst — O(1)
stack.pop(); // removeFirst — O(1)
stack.peek(); // peekFirst — O(1)
// Queue semantics (same class, different end)
Deque<Integer> queue = new ArrayDeque<>();
queue.offerLast(x); // enqueue — O(1)
queue.pollFirst(); // dequeue — O(1)
ArrayDeque is the right default for both stacks and queues. Pick the end you use; the class handles both.
Monotonic stack skeleton (Java):
Deque<Integer> mono = new ArrayDeque<>();
for (int i = 0; i < n; i++) {
// Decreasing: pop while top is smaller than current (next-greater pattern)
while (!mono.isEmpty() && arr[mono.peek()] < arr[i]) {
int idx = mono.pop();
// arr[idx]'s next greater element is at i; distance = i - idx
}
mono.push(i); // always store index, not value
}
Swap < for > to get a next-smaller / increasing-stack variant.
Store indices, not values, whenever you need distance or width. arr[mono.peek()] gives the value; the index gives span or gap.
Min stack (Java pair approach):
Deque<int[]> stack = new ArrayDeque<>();
// push: [val, currentMin]
int newMin = stack.isEmpty() ? val : Math.min(val, stack.peek()[1]);
stack.push(new int[]{val, newMin});
// getMin: stack.peek()[1]
Frequency stack (Java):
Map<Integer, Integer> freq = new HashMap<>();
Map<Integer, Deque<Integer>> group = new HashMap<>();
int maxFreq = 0;
// push
freq.merge(val, 1, Integer::sum);
int f = freq.get(val);
maxFreq = Math.max(maxFreq, f);
group.computeIfAbsent(f, k -> new ArrayDeque<>()).push(val);
// pop
int val = group.get(maxFreq).pop();
freq.merge(val, -1, Integer::sum);
if (group.get(maxFreq).isEmpty()) maxFreq--;
Per-Problem Notes
| Problem | Key Insight | Tricky Part | Common Mistakes | Time | Space |
|---|---|---|---|---|---|
| Baseball Game (682) | Simulation: C=pop, D=double top, +=sum top two, else push int |
+ does NOT pop the two values it reads |
Treating + as destructive |
O(n) | O(n) |
| Valid Parentheses (20) | Push open; on close pop and check match against closer map | Stack may be empty when a close arrives; stack may be non-empty at end | Forgetting end-of-string empty check | O(n) | O(n) |
| Implement Stack Using Queues (225) | Costly-push: rotate entire queue after each push so front = top | Single queue is sufficient; no need for two | Using two queues unnecessarily | O(n) push, O(1) pop | O(n) |
| Implement Queue Using Stacks (232) | Inbox/outbox; transfer only when outbox is empty (lazy) | Transfer is O(n) worst case but each element moves at most once | Transferring on every dequeue | O(1) amortized | O(n) |
| Min Stack (155) | Each stack frame stores [val, currentMin]; min is always at peek |
Single global min variable breaks when the minimum is popped |
Using a separate unsynced min variable | O(1) all ops | O(n) |
| Evaluate RPN (150) | Single operand stack; pop two on operator; push result | Operand order matters: b op a where a = stack.pop() first, b = stack.pop() second |
Reversed operand order for - and /; Python // rounds toward −∞, use int(a/b) |
O(n) | O(n) |
| Asteroid Collision (735) | Positive → push; negative → destroy positives on top in a loop; equal-size → both gone | Negative asteroid survives only after destroying everything on stack; still push if stack empty or top is negative | Missing inner loop; equal-size explosion | O(n) | O(n) |
| Daily Temperatures (739) | Monotonic decreasing stack of indices; pop when current temp > stack top | Distance = i - popped_index; only possible if indices are stored |
Storing temperatures instead of indices | O(n) | O(n) |
| Online Stock Span (901) | Stack of (price, span); when top price ≤ current, absorb its span and pop |
Accumulated span, not just count of pops | Counting pops instead of summing spans | O(1) amortized | O(n) |
| Car Fleet (853) | Sort by position descending; compute time = (target - pos) / speed; if time ≤ top of stack it merges, else push |
Equal times also merge (same fleet) | Sorting ascending; missing the equal-time merge | O(n log n) | O(n) |
| Simplify Path (71) | Split on /; skip empty strings and .; pop on ..; push everything else |
Multiple consecutive slashes produce empty tokens (skip them); leading / must be re-added |
Not handling .. at root; missing leading slash in output |
O(n) | O(n) |
| Decode String (394) | Two stacks: one for repeat counts, one for string prefixes; on [ push state; on ] pop and repeat |
Multi-digit numbers require accumulating digits before pushing | Treating multi-digit numbers as single digits; off-by-one on bracket matching | O(n·k) | O(n) |
| Maximum Frequency Stack (895) | freq map + group map (freq → stack of vals); maxFreq tracks current peak |
maxFreq decrements only when its group becomes empty after a pop |
Forgetting to decrement maxFreq; not handling tie-breaking (recency already handled by stack order within group) |
O(1) push/pop | O(n) |
| Largest Rectangle in Histogram (84) | Monotonic increasing stack of (start_index, height); when shorter bar arrives, pop and compute area = height × (i - start_index); start carried back to leftmost extent |
Width calculation: popped bar extends left to wherever it could have started, not just its original index | Not flushing remaining stack after loop; width off-by-one | O(n) | O(n) |
Edge Cases to Watch
-
Empty stack pop — always check
if stack(Python) or!stack.isEmpty()(Java) beforepop()/peek(). Applies everywhere: asteroid collision inner loop, RPN operators, close-bracket in valid parentheses. -
All same elements in monotonic stack — invariant never breaks during the loop, so the stack accumulates every element. These must be processed in a final flush pass after the loop (as in largest rectangle).
-
Nested brackets in decode_string — each
[must push the entire current string and the current count onto their respective stacks, not just a reference. Restoring state on]means popping both stacks and concatenating. -
Negative numbers in asteroid_collision — a right-moving (positive) asteroid followed by a left-moving (negative) one of the same magnitude: both are destroyed. A negative asteroid moving left when the stack is empty or the top is also negative: it survives and is pushed as-is.
-
Stack not empty at end means invalid — for valid_parentheses, any unmatched open bracket left on the stack at the end of the string is an error. Return
len(stack) == 0, not just whether a close mismatched. -
Stack-as-queue amortized vs worst case — implement_queue_using_stacks is O(1) amortized per dequeue, but a single dequeue after n pushes costs O(n). This is fine for interviews but matters for latency-sensitive code where worst-case matters more than average.
-
Path traversal past root — in simplify_path,
..on an empty stack should be a no-op, not an error. The result is still/. -
Sentinel trick in histogram — appending a
0height sentinel toheightsbefore iterating lets you flush the stack in one pass without a separate post-loop sweep. The trade-off: slightly less obvious code for simpler loop structure.
| # | Problem | Difficulty | Category |
|---|---|---|---|
| 1 | Baseball Game (LC 682) | Easy | Simulation |
| 2 | Valid Parentheses (LC 20) | Easy | Matching |
| 3 | Implement Stack Using Queues (LC 225) | Easy | Design |
| 4 | Implement Queue using Stacks (LC 232) | Easy | Design |
| 5 | Min Stack (LC 155) | Medium | Design |
| 6 | Evaluate Reverse Polish Notation (LC 150) | Medium | Simulation |
| 7 | Asteroid Collision (LC 735) | Medium | Simulation |
| 8 | Daily Temperatures (LC 739) | Medium | Monotonic |
| 9 | Online Stock Span (LC 901) | Medium | Monotonic |
| 10 | Car Fleet (LC 853) | Medium | Monotonic |
| 11 | Simplify Path (LC 71) | Medium | Simulation |
| 12 | Decode String (LC 394) | Medium | Simulation |
| 13 | Maximum Frequency Stack (LC 895) | Hard | Design |
| 14 | Largest Rectangle In Histogram (LC 84) | Hard | Monotonic |
Asteroid Collision PythonJava ▼
"""
# 735. Asteroid Collision
## Problem
We are given an array `asteroids` of integers representing asteroids in a row.
For each asteroid, the absolute value represents its size, and the sign
represents its direction (positive = right, negative = left). All asteroids
move at the same speed.
Find out the state of the asteroids after all collisions. If two asteroids
meet, the smaller one explodes. If both are the same size, both explode. Two
asteroids moving in the same direction never meet.
## Examples
```text
Input: asteroids = [5, 10, -5]
Output: [5, 10]
Explanation: 10 and -5 collide -> 10 survives. 5 and 10 never collide.
Input: asteroids = [8, -8]
Output: []
Explanation: 8 and -8 collide -> both explode.
Input: asteroids = [10, 2, -5]
Output: [10]
Explanation: 2 and -5 collide -> -5 wins. 10 and -5 collide -> 10 wins.
```
## Constraints
- `2 <= asteroids.length <= 10^4`
- `-1000 <= asteroids[i] <= 1000`
- `asteroids[i] != 0`
"""
from typing import List
class Solution:
def asteroidCollision(self, asteroids: List[int]) -> List[int]:
# Only collisions happen when a left-moving asteroid meets a right-moving one on stack
# Keep destroying top of stack while it's smaller than the incoming asteroid
# Equal sizes destroy both; larger stack top destroys incoming
# O(n) time, O(n) space — stack simulation of collisions
stack = []
for a in asteroids:
alive = True
while alive and a < 0 and stack and stack[-1] > 0:
if stack[-1] < -a:
stack.pop() # right asteroid destroyed
elif stack[-1] == -a:
stack.pop() # both destroyed
alive = False
else:
alive = False # left asteroid destroyed
if alive:
stack.append(a)
return stack
if __name__ == "__main__":
s = Solution()
assert s.asteroidCollision([5, 10, -5]) == [5, 10]
assert s.asteroidCollision([8, -8]) == []
assert s.asteroidCollision([10, 2, -5]) == [10]
assert s.asteroidCollision([-2, -1, 1, 2]) == [-2, -1, 1, 2]
assert s.asteroidCollision([1, -1]) == []
print("All tests passed.") Baseball Game PythonJava ▼
"""
# 682. Baseball Game
## Problem
You are keeping score for a baseball game with special rules. Given a list of
operations, calculate the sum of all valid scores.
Operations:
- Integer string: record a new score of that value
- `"+"`: record a new score that is the sum of the previous two scores
- `"D"`: record a new score that is double the previous score
- `"C"`: invalidate the previous score, removing it from the record
## Examples
```text
Input: operations = ["5","2","C","D","+"]
Output: 30
Explanation:
"5" -> record [5]
"2" -> record [5, 2]
"C" -> remove last -> [5]
"D" -> double last -> [5, 10]
"+" -> sum last two -> [5, 10, 15]
Total = 5 + 10 + 15 = 30
Input: operations = ["5","-2","4","C","D","9","+","+"]
Output: 27
```
## Constraints
- `1 <= operations.length <= 1000`
- `operations[i]` is `"C"`, `"D"`, `"+"`, or a string representing an integer
in the range `[-3 * 10^4, 3 * 10^4]`
- For `"+"`, there will always be at least two previous scores
- For `"C"` and `"D"`, there will always be at least one previous score
"""
from typing import List
class Solution:
def calPoints_list(self, operations: List[str]) -> int:
# Simulate the record: C removes last, D doubles last, + sums last two
# O(n) time, O(n) space — list as mutable record
record = []
for op in operations:
if op == "C":
record.pop()
elif op == "D":
record.append(record[-1] * 2)
elif op == "+":
record.append(record[-1] + record[-2])
else:
record.append(int(op))
return sum(record)
def calPoints(self, operations: List[str]) -> int:
# Same logic with explicit stack name for clarity
# O(n) time, O(n) space — explicit stack (same logic, named for clarity)
stack = []
for op in operations:
if op == "C":
stack.pop()
elif op == "D":
stack.append(stack[-1] * 2)
elif op == "+":
stack.append(stack[-1] + stack[-2])
else:
stack.append(int(op))
return sum(stack)
if __name__ == "__main__":
s = Solution()
assert s.calPoints(["5", "2", "C", "D", "+"]) == 30
assert s.calPoints(["5", "-2", "4", "C", "D", "9", "+", "+"]) == 27
assert s.calPoints(["1"]) == 1
print("All tests passed.") Car Fleet PythonJava ▼
"""
# 853. Car Fleet
## Problem
There are `n` cars at given miles away from the starting point, all driving
towards the same `target` destination. Each car `i` has a position
`position[i]` and speed `speed[i]` (in miles per hour).
A car can never pass another car ahead of it, but it can catch up to it and
drive bumper to bumper at the slower car's speed. Two cars that drive bumper
to bumper are considered a single car fleet.
Return the number of car fleets that will arrive at the destination.
## Examples
```text
Input: target = 12, position = [10,8,0,5,3], speed = [2,4,1,1,3]
Output: 3
Explanation:
Cars at 10 and 8 become a fleet (arrive at 12 at time 1 and 1).
Car at 0 is its own fleet (arrives at time 12).
Cars at 5 and 3 become a fleet (arrive at time 7 and 3 -> 3 catches 5).
Input: target = 10, position = [3], speed = [3]
Output: 1
Input: target = 100, position = [0,2,4], speed = [4,2,1]
Output: 1
```
## Constraints
- `n == position.length == speed.length`
- `1 <= n <= 10^5`
- `0 < target <= 10^6`
- `0 <= position[i] < target`
- All values of `position` are unique
- `0 < speed[i] <= 10^6`
"""
from typing import List
class Solution:
def carFleet(self, target: int, position: List[int], speed: List[int]) -> int:
# Sort cars by position descending (closest to target first)
# Compute arrival time for each car; if it arrives before the car ahead, it joins that fleet
# A new fleet forms only when a car arrives strictly later than the current top
# O(n log n) time, O(n) space — sort by position, stack of arrival times
pairs = sorted(zip(position, speed), reverse=True)
stack = []
for pos, spd in pairs:
time = (target - pos) / spd
if not stack or time > stack[-1]:
stack.append(time)
# if time <= stack[-1], this car catches the fleet ahead -> same fleet
return len(stack)
if __name__ == "__main__":
s = Solution()
assert s.carFleet(12, [10, 8, 0, 5, 3], [2, 4, 1, 1, 3]) == 3
assert s.carFleet(10, [3], [3]) == 1
assert s.carFleet(100, [0, 2, 4], [4, 2, 1]) == 1
assert s.carFleet(10, [6, 8], [3, 2]) == 2
print("All tests passed.") Daily Temperatures PythonJava ▼
"""
# 739. Daily Temperatures
## Problem
Given an array of integers `temperatures` representing daily temperatures,
return an array `answer` such that `answer[i]` is the number of days you have
to wait after the `i`-th day to get a warmer temperature. If there is no
future day with a warmer temperature, `answer[i] == 0`.
## Examples
```text
Input: temperatures = [73,74,75,71,69,72,76,73]
Output: [1,1,4,2,1,1,0,0]
Input: temperatures = [30,40,50,60]
Output: [1,1,1,0]
Input: temperatures = [30,60,90]
Output: [1,1,0]
```
## Constraints
- `1 <= temperatures.length <= 10^5`
- `30 <= temperatures[i] <= 100`
"""
from typing import List
class Solution:
def dailyTemperatures_brute(self, temperatures: List[int]) -> List[int]:
# For each day, scan forward until a warmer day is found — O(n^2)
# O(n^2) time, O(1) extra space — check every future day
n = len(temperatures)
answer = [0] * n
for i in range(n):
for j in range(i + 1, n):
if temperatures[j] > temperatures[i]:
answer[i] = j - i
break
return answer
def dailyTemperatures(self, temperatures: List[int]) -> List[int]:
# Monotonic decreasing stack stores indices of unresolved days
# When a warmer day arrives, resolve all cooler days on the stack
# O(n) time, O(n) space — decreasing monotonic stack of indices
n = len(temperatures)
answer = [0] * n
stack = [] # indices with unresolved warmer day
for i, t in enumerate(temperatures):
while stack and temperatures[stack[-1]] < t:
idx = stack.pop()
answer[idx] = i - idx
stack.append(i)
return answer
if __name__ == "__main__":
s = Solution()
assert s.dailyTemperatures([73, 74, 75, 71, 69, 72, 76, 73]) == [1, 1, 4, 2, 1, 1, 0, 0]
assert s.dailyTemperatures([30, 40, 50, 60]) == [1, 1, 1, 0]
assert s.dailyTemperatures([30, 60, 90]) == [1, 1, 0]
assert s.dailyTemperatures_brute([73, 74, 75, 71, 69, 72, 76, 73]) == [1, 1, 4, 2, 1, 1, 0, 0]
print("All tests passed.") Decode String PythonJava ▼
"""
# 394. Decode String
## Problem
Given an encoded string, return its decoded string.
The encoding rule is: `k[encoded_string]`, where the `encoded_string` inside
the square brackets is repeated exactly `k` times. `k` is guaranteed to be a
positive integer. You may assume the input is always valid — no extra white
spaces, square brackets are well-formed, etc.
Note: The original data does not contain any digits; all digits are only for
repeat counts `k`. For example, there won't be input like `3a` or `2[4]`.
## Examples
```text
Input: s = "3[a]2[bc]"
Output: "aaabcbc"
Input: s = "3[a2[c]]"
Output: "accaccacc"
Input: s = "2[abc]3[cd]ef"
Output: "abcabccdcdcdef"
```
## Constraints
- `1 <= s.length <= 30`
- `s` consists of lowercase English letters, digits, and square brackets `[]`
- `s` is guaranteed to be a valid input
- All integers are in the range `[1, 300]`
"""
class Solution:
def decodeString_stack(self, s: str) -> str:
# On '[': save current (count, string) to stack, reset both
# On ']': pop saved state, append current string repeated count times
# Handles nested brackets because each level is saved/restored on stack
# O(n) time, O(n) space — iterative stack of (repeat_count, built_string)
stack = []
cur_str = ""
cur_num = 0
for ch in s:
if ch.isdigit():
cur_num = cur_num * 10 + int(ch)
elif ch == "[":
stack.append((cur_num, cur_str))
cur_str, cur_num = "", 0
elif ch == "]":
num, prev_str = stack.pop()
cur_str = prev_str + cur_str * num
else:
cur_str += ch
return cur_str
def decodeString(self, s: str) -> str:
# Recursive descent: parse number, skip '[', recurse for inner, skip ']'
# Returns (decoded_string, next_index) so caller knows where to continue
# O(n) time, O(n) space — recursive descent with index pointer
def decode(i: int):
result = ""
while i < len(s) and s[i] != "]":
if s[i].isdigit():
num = 0
while s[i].isdigit():
num = num * 10 + int(s[i])
i += 1
i += 1 # skip '['
inner, i = decode(i)
i += 1 # skip ']'
result += inner * num
else:
result += s[i]
i += 1
return result, i
return decode(0)[0]
if __name__ == "__main__":
s = Solution()
assert s.decodeString_stack("3[a]2[bc]") == "aaabcbc"
assert s.decodeString_stack("3[a2[c]]") == "accaccacc"
assert s.decodeString_stack("2[abc]3[cd]ef") == "abcabccdcdcdef"
assert s.decodeString("3[a]2[bc]") == "aaabcbc"
assert s.decodeString("3[a2[c]]") == "accaccacc"
assert s.decodeString("2[abc]3[cd]ef") == "abcabccdcdcdef"
print("All tests passed.") Evaluate Reverse Polish Notation PythonJava ▼
"""
# 150. Evaluate Reverse Polish Notation
## Problem
Evaluate the value of an arithmetic expression in Reverse Polish Notation (RPN).
Valid operators are `+`, `-`, `*`, and `/`. Each operand may be an integer or
another expression. Division truncates toward zero.
## Examples
```text
Input: tokens = ["2","1","+","3","*"]
Output: 9
Explanation: ((2 + 1) * 3) = 9
Input: tokens = ["4","13","5","/","+"]
Output: 6
Explanation: (4 + (13 / 5)) = 6
Input: tokens = ["10","6","9","3","+","-11","*","/","*","17","+","5","+"]
Output: 22
```
## Constraints
- `1 <= tokens.length <= 10^4`
- `tokens[i]` is either an operator `+`, `-`, `*`, `/`, or an integer in the
range `[-200, 200]`
- The given RPN expression is always valid
"""
from typing import List
class Solution:
def evalRPN(self, tokens: List[str]) -> int:
# Push numbers; on operator, pop two operands and push result
# Use int(a/b) instead of a//b to truncate toward zero (not floor)
# O(n) time, O(n) space — single stack evaluation
stack = []
ops = {
"+": lambda a, b: a + b,
"-": lambda a, b: a - b,
"*": lambda a, b: a * b,
"/": lambda a, b: int(a / b), # truncate toward zero
}
for t in tokens:
if t in ops:
b, a = stack.pop(), stack.pop()
stack.append(ops[t](a, b))
else:
stack.append(int(t))
return stack[0]
if __name__ == "__main__":
s = Solution()
assert s.evalRPN(["2", "1", "+", "3", "*"]) == 9
assert s.evalRPN(["4", "13", "5", "/", "+"]) == 6
assert s.evalRPN(["10", "6", "9", "3", "+", "-11", "*", "/", "*", "17", "+", "5", "+"]) == 22
assert s.evalRPN(["3", "-4", "/"]) == 0 # truncate toward zero: -0.75 -> 0
print("All tests passed.") Implement Queue Using Stacks PythonJava ▼
"""
# 232. Implement Queue using Stacks
## Problem
Implement a first-in-first-out (FIFO) queue using only two stacks. The
implemented queue should support `push`, `pop`, `peek`, and `empty` operations.
You must use **only** standard stack operations: `push to top`, `peek/pop from
top`, `size`, and `is empty`.
## Operations
- `push(x)` — Push element `x` to the back of the queue.
- `pop()` — Removes the element from the front of the queue and returns it.
- `peek()` — Returns the element at the front of the queue.
- `empty()` — Returns `true` if the queue is empty, `false` otherwise.
## Examples
```text
Input:
["MyQueue","push","push","peek","pop","empty"]
[[],[1],[2],[],[],[]]
Output:
[null,null,null,1,1,false]
```
## Constraints
- `1 <= x <= 9`
- At most `100` calls will be made to `push`, `pop`, `peek`, and `empty`
- All calls to `pop` and `peek` are valid
"""
class MyQueue:
"""Approach 1: Two stacks, costly push — O(n) push, O(1) pop/peek."""
def __init__(self):
self.s1: list = [] # front stack
self.s2: list = [] # temp stack
def push(self, x: int) -> None:
# Move all elements to s2, push new element to bottom of s1, move back
# O(n) — move all to s2, push x to s1 (bottom), move back
while self.s1:
self.s2.append(self.s1.pop())
self.s1.append(x)
while self.s2:
self.s1.append(self.s2.pop())
def pop(self) -> int:
# Front element is always at top of s1 after costly push
# O(1)
return self.s1.pop()
def peek(self) -> int:
# Peek top of s1 which holds the front of the queue
# O(1)
return self.s1[-1]
def empty(self) -> bool:
# Queue is empty when s1 is empty
# O(1)
return not self.s1
class MyQueueAmortized:
"""Approach 2: Two stacks, amortized O(1) — lazy transfer on pop/peek."""
def __init__(self):
self.inbox: list = []
self.outbox: list = []
def push(self, x: int) -> None:
# Move all elements to s2, push new element to bottom of s1, move back
# O(1)
self.inbox.append(x)
def _transfer(self) -> None:
# Lazy transfer: only move inbox to outbox when outbox is empty
if not self.outbox:
while self.inbox:
self.outbox.append(self.inbox.pop())
def pop(self) -> int:
# Front element is always at top of s1 after costly push
# O(1) amortized
self._transfer()
return self.outbox.pop()
def peek(self) -> int:
# Peek top of s1 which holds the front of the queue
# O(1) amortized
self._transfer()
return self.outbox[-1]
def empty(self) -> bool:
# Queue is empty when s1 is empty
# O(1)
return not self.inbox and not self.outbox
if __name__ == "__main__":
for Cls in (MyQueue, MyQueueAmortized):
q = Cls()
q.push(1)
q.push(2)
assert q.peek() == 1
assert q.pop() == 1
assert q.empty() is False
assert q.pop() == 2
assert q.empty() is True
print("All tests passed.") Implement Stack Using Queues PythonJava ▼
"""
# 225. Implement Stack using Queues
## Problem
Implement a last-in-first-out (LIFO) stack using only two queues. The
implemented stack should support `push`, `pop`, `top`, and `empty` operations.
You must use **only** standard queue operations: `push to back`, `peek/pop from
front`, `size`, and `is empty`.
## Operations
- `push(x)` — Push element `x` onto stack.
- `pop()` — Removes the element on top of the stack and returns it.
- `top()` — Get the top element.
- `empty()` — Returns `true` if the stack is empty, `false` otherwise.
## Examples
```text
Input:
["MyStack","push","push","top","pop","empty"]
[[],[1],[2],[],[],[]]
Output:
[null,null,null,2,2,false]
```
## Constraints
- `1 <= x <= 9`
- At most `100` calls will be made to `push`, `pop`, `top`, and `empty`
- All calls to `pop` and `top` are valid
"""
from collections import deque
class MyStack:
"""Approach 1: Two queues, costly push — O(n) push, O(1) pop/top."""
def __init__(self):
self.q1: deque = deque()
self.q2: deque = deque()
def push(self, x: int) -> None:
# Rotate all existing elements behind the new one so it's at front
# O(n) — rotate all existing elements through q2 so x ends up at front
self.q2.append(x)
while self.q1:
self.q2.append(self.q1.popleft())
self.q1, self.q2 = self.q2, self.q1
def pop(self) -> int:
# Front of q1 is always the most recently pushed element
# O(1)
return self.q1.popleft()
def top(self) -> int:
# Peek front of q1
# O(1)
return self.q1[0]
def empty(self) -> bool:
# Stack is empty when q1 is empty
# O(1)
return not self.q1
class MyStackCostlyPop:
"""Approach 2: Two queues, costly pop — O(1) push, O(n) pop/top."""
def __init__(self):
self.q1: deque = deque()
self.q2: deque = deque()
def push(self, x: int) -> None:
# Rotate all existing elements behind the new one so it's at front
# O(1)
self.q1.append(x)
def pop(self) -> int:
# Front of q1 is always the most recently pushed element
# O(n) — drain q1 into q2 leaving last element
while len(self.q1) > 1:
self.q2.append(self.q1.popleft())
val = self.q1.popleft()
self.q1, self.q2 = self.q2, self.q1
return val
def top(self) -> int:
# Peek front of q1
# O(n)
while len(self.q1) > 1:
self.q2.append(self.q1.popleft())
val = self.q1[0]
self.q2.append(self.q1.popleft())
self.q1, self.q2 = self.q2, self.q1
return val
def empty(self) -> bool:
# Stack is empty when q1 is empty
# O(1)
return not self.q1
if __name__ == "__main__":
for Cls in (MyStack, MyStackCostlyPop):
st = Cls()
st.push(1)
st.push(2)
assert st.top() == 2
assert st.pop() == 2
assert st.empty() is False
assert st.pop() == 1
assert st.empty() is True
print("All tests passed.") Largest Rectangle In Histogram PythonJava ▼
"""
# 84. Largest Rectangle in Histogram
## Problem
Given an array of integers `heights` representing the histogram's bar heights
where the width of each bar is `1`, return the area of the largest rectangle
in the histogram.
## Examples
```text
Input: heights = [2,1,5,6,2,3]
Output: 10
Explanation: The largest rectangle has area 10 (bars of height 5 and 6,
width 2).
Input: heights = [2,4]
Output: 4
```
## Constraints
- `1 <= heights.length <= 10^5`
- `0 <= heights[i] <= 10^4`
"""
from typing import List
class Solution:
def largestRectangleArea_brute(self, heights: List[int]) -> int:
# For each starting bar, extend right tracking minimum height — O(n^2)
# O(n^2) time, O(1) extra space — expand each bar left and right
n = len(heights)
max_area = 0
for i in range(n):
min_h = heights[i]
for j in range(i, n):
min_h = min(min_h, heights[j])
max_area = max(max_area, min_h * (j - i + 1))
return max_area
def largestRectangleArea(self, heights: List[int]) -> int:
# Monotonic increasing stack: when a shorter bar is found, pop taller bars
# Each popped bar's rectangle extends from its stored start index to current i
# 'start' tracks how far left the current bar can extend
# O(n) time, O(n) space — increasing monotonic stack with sentinel
stack = [] # (index, height) — index = leftmost extent of current bar
max_area = 0
for i, h in enumerate(heights):
start = i
while stack and stack[-1][1] > h:
idx, height = stack.pop()
max_area = max(max_area, height * (i - idx))
start = idx
stack.append((start, h))
for idx, height in stack:
max_area = max(max_area, height * (len(heights) - idx))
return max_area
if __name__ == "__main__":
s = Solution()
assert s.largestRectangleArea([2, 1, 5, 6, 2, 3]) == 10
assert s.largestRectangleArea([2, 4]) == 4
assert s.largestRectangleArea([1]) == 1
assert s.largestRectangleArea([6, 2, 5, 4, 5, 1, 6]) == 12
assert s.largestRectangleArea_brute([2, 1, 5, 6, 2, 3]) == 10
assert s.largestRectangleArea_brute([2, 4]) == 4
print("All tests passed.") Maximum Frequency Stack PythonJava ▼
"""
# 895. Maximum Frequency Stack
## Problem
Design a stack-like data structure that pushes and pops elements according to
a maximum frequency rule.
Implement the `FreqStack` class:
- `push(val)` — Pushes an integer `val` onto the stack.
- `pop()` — Removes and returns the most frequent element in the stack.
- If there is a tie for the most frequent element, the element closest to
the top of the stack (most recently pushed) is removed and returned.
## Examples
```text
Input:
["FreqStack","push","push","push","push","push","push","pop","pop","pop","pop"]
[[],[5],[7],[5],[7],[4],[5],[],[],[],[]]
Output:
[null,null,null,null,null,null,null,5,7,5,4]
Explanation:
After pushes: freq = {5:3, 7:2, 4:1}
pop() -> 5 (freq 3, most frequent)
pop() -> 7 (freq 2 tie between 5 and 7; 7 was pushed more recently at freq 2)
pop() -> 5 (freq 2)
pop() -> 4 (freq 1 tie; 4 was pushed most recently at freq 1)
```
## Constraints
- `0 <= val <= 10^9`
- At most `2 * 10^4` calls will be made to `push` and `pop`
- It is guaranteed that there will be at least one element in the stack before
calling `pop`
"""
from collections import defaultdict
class FreqStack:
"""Approach 1: freq map + group map keyed by frequency — O(1) push and pop."""
def __init__(self):
self.freq: dict = defaultdict(int) # val -> frequency
self.group: dict = defaultdict(list) # freq -> stack of vals
self.max_freq: int = 0
def push(self, val: int) -> None:
# Increment frequency of val; append to the group for that frequency
# Update max_freq if this val's frequency is now the highest
# O(1) — increment freq, append to corresponding group
self.freq[val] += 1
f = self.freq[val]
self.max_freq = max(self.max_freq, f)
self.group[f].append(val)
def pop(self) -> int:
# Pop from the group at max_freq (most recently pushed among most frequent)
# Decrement val's frequency; if max_freq group is now empty, decrease max_freq
# O(1) — pop from max_freq group, decrement freq
val = self.group[self.max_freq].pop()
self.freq[val] -= 1
if not self.group[self.max_freq]:
self.max_freq -= 1
return val
if __name__ == "__main__":
fs = FreqStack()
for v in [5, 7, 5, 7, 4, 5]:
fs.push(v)
assert fs.pop() == 5
assert fs.pop() == 7
assert fs.pop() == 5
assert fs.pop() == 4
print("All tests passed.") Min Stack PythonJava ▼
"""
# 155. Min Stack
## Problem
Design a stack that supports `push`, `pop`, `top`, and retrieving the minimum
element — all in **O(1)** time.
## Operations
- `push(val)` — Push element `val` onto the stack.
- `pop()` — Removes the element on top of the stack.
- `top()` — Gets the top element.
- `getMin()` — Retrieves the minimum element in the stack.
## Examples
```text
Input:
["MinStack","push","push","push","getMin","pop","top","getMin"]
[[],[-2],[0],[-3],[],[],[],[]]
Output:
[null,null,null,null,-3,null,0,-2]
```
## Constraints
- `-2^31 <= val <= 2^31 - 1`
- Methods `pop`, `top`, and `getMin` will always be called on non-empty stacks
- At most `3 * 10^4` calls will be made to `push`, `pop`, `top`, and `getMin`
"""
class MinStack:
"""Approach 1: Two stacks — main stack + parallel min-tracking stack."""
def __init__(self):
self.stack: list = []
self.min_stack: list = []
def push(self, val: int) -> None:
# Push value to main stack; also push new min to min_stack
# O(1)
self.stack.append(val)
self.min_stack.append(min(val, self.min_stack[-1] if self.min_stack else val))
def pop(self) -> None:
# Pop from both stacks to keep them in sync
# O(1)
self.stack.pop()
self.min_stack.pop()
def top(self) -> int:
# Peek top of main stack
# O(1)
return self.stack[-1]
def getMin(self) -> int:
# Peek top of min_stack — always holds current minimum
# O(1)
return self.min_stack[-1]
class MinStackTuple:
"""Approach 2: Single stack of (value, current_min) tuples."""
def __init__(self):
self.stack: list = [] # each entry: (val, current_min)
def push(self, val: int) -> None:
# Push value to main stack; also push new min to min_stack
# O(1)
cur_min = min(val, self.stack[-1][1]) if self.stack else val
self.stack.append((val, cur_min))
def pop(self) -> None:
# Pop from both stacks to keep them in sync
# O(1)
self.stack.pop()
def top(self) -> int:
# Peek top of main stack
# O(1)
return self.stack[-1][0]
def getMin(self) -> int:
# Peek top of min_stack — always holds current minimum
# O(1)
return self.stack[-1][1]
if __name__ == "__main__":
for Cls in (MinStack, MinStackTuple):
ms = Cls()
ms.push(-2)
ms.push(0)
ms.push(-3)
assert ms.getMin() == -3
ms.pop()
assert ms.top() == 0
assert ms.getMin() == -2
print("All tests passed.") Online Stock Span PythonJava ▼
"""
# 901. Online Stock Span
## Problem
Design an algorithm that collects daily price quotes for some stock and returns
the **span** of that stock's price for the current day.
The span of the stock's price today is defined as the maximum number of
consecutive days (starting from today and going backwards) for which the stock
price was less than or equal to today's price.
## Examples
```text
Input:
["StockSpanner","next","next","next","next","next","next","next"]
[[],[100],[80],[60],[70],[60],[75],[85]]
Output:
[null,1,1,1,2,1,4,6]
Explanation:
next(100) -> 1 (no previous days)
next(80) -> 1 (80 < 100)
next(60) -> 1 (60 < 80)
next(70) -> 2 (70 >= 60, but 70 < 80)
next(60) -> 1
next(75) -> 4 (75 >= 60, 70, 60, but 75 < 80)
next(85) -> 6 (85 >= 75, 60, 70, 60, 80, but 85 < 100)
```
## Constraints
- `1 <= price <= 10^5`
- At most `10^4` calls will be made to `next`
"""
class StockSpanner:
"""Approach 1: Brute force — scan backwards on each call."""
def __init__(self):
# O(n) per call, O(n) space
self.prices: list = []
def next(self, price: int) -> int:
# Collapse all previous days with price <= current into one span
# Stack stores (price, span) pairs; accumulate spans of dominated days
# O(n) time — linear scan back through history
self.prices.append(price)
span = 1
i = len(self.prices) - 2
while i >= 0 and self.prices[i] <= price:
span += 1
i -= 1
return span
class StockSpannerOptimal:
"""Approach 2: Monotonic stack — O(1) amortized per call."""
def __init__(self):
self.stack: list = [] # (price, span)
def next(self, price: int) -> int:
# Collapse all previous days with price <= current into one span
# Stack stores (price, span) pairs; accumulate spans of dominated days
# O(1) amortized — collapse dominated entries into accumulated span
span = 1
while self.stack and self.stack[-1][0] <= price:
span += self.stack.pop()[1]
self.stack.append((price, span))
return span
if __name__ == "__main__":
for Cls in (StockSpanner, StockSpannerOptimal):
sp = Cls()
assert sp.next(100) == 1
assert sp.next(80) == 1
assert sp.next(60) == 1
assert sp.next(70) == 2
assert sp.next(60) == 1
assert sp.next(75) == 4
assert sp.next(85) == 6
print("All tests passed.") Simplify Path PythonJava ▼
"""
# 71. Simplify Path
## Problem
Given an absolute path for a Unix-style file system, which begins with a
slash `'/'`, transform this path into its simplified canonical path.
Rules:
- A single period `'.'` refers to the current directory.
- A double period `'..'` refers to the parent directory.
- Multiple consecutive slashes are treated as a single slash.
- Any other string is a valid directory or file name.
The canonical path must:
- Start with a single slash `'/'`
- Not end with a trailing slash (unless it is the root)
- Not have consecutive slashes
- Not contain `.` or `..`
## Examples
```text
Input: path = "/home/"
Output: "/home"
Input: path = "/../"
Output: "/"
Input: path = "/home//foo/"
Output: "/home/foo"
Input: path = "/a/./b/../../c/"
Output: "/c"
```
## Constraints
- `1 <= path.length <= 3000`
- `path` consists of English letters, digits, period `'.'`, slash `'/'`, or
underscore `'_'`
- `path` is a valid absolute Unix path
"""
class Solution:
def simplifyPath(self, path: str) -> str:
# Split on '/' to get path components; skip empty strings and '.'
# '..' pops the last directory from stack (go up one level)
# Join remaining stack with '/' and prepend root '/'
# O(n) time, O(n) space — split on '/', process with stack
stack = []
for part in path.split("/"):
if part == "..":
if stack:
stack.pop()
elif part and part != ".":
stack.append(part)
return "/" + "/".join(stack)
if __name__ == "__main__":
s = Solution()
assert s.simplifyPath("/home/") == "/home"
assert s.simplifyPath("/../") == "/"
assert s.simplifyPath("/home//foo/") == "/home/foo"
assert s.simplifyPath("/a/./b/../../c/") == "/c"
assert s.simplifyPath("/") == "/"
print("All tests passed.") Valid Parentheses PythonJava ▼
"""
# 20. Valid Parentheses
## Problem
Given a string `s` containing only the characters `'('`, `')'`, `'{'`, `'}'`,
`'['`, and `']'`, determine if the input string is valid.
A string is valid if:
1. Open brackets are closed by the same type of brackets.
2. Open brackets are closed in the correct order.
3. Every close bracket has a corresponding open bracket of the same type.
## Examples
```text
Input: s = "()"
Output: true
Input: s = "()[]{}"
Output: true
Input: s = "(]"
Output: false
Input: s = "([)]"
Output: false
Input: s = "{[]}"
Output: true
```
## Constraints
- `1 <= s.length <= 10^4`
- `s` consists of parentheses only: `'()[]{}'`
"""
class Solution:
def isValid(self, s: str) -> bool:
# Push opening brackets; on closing bracket, check top matches
# Stack empty at end means all brackets were properly closed
# O(n) time, O(n) space — stack-based bracket matching
stack = []
match = {")": "(", "}": "{", "]": "["}
for ch in s:
if ch in match:
if not stack or stack[-1] != match[ch]:
return False
stack.pop()
else:
stack.append(ch)
return not stack
if __name__ == "__main__":
s = Solution()
assert s.isValid("()") is True
assert s.isValid("()[]{}") is True
assert s.isValid("(]") is False
assert s.isValid("([)]") is False
assert s.isValid("{[]}") is True
assert s.isValid("") is True
print("All tests passed.") Stack
Monotonic stack · Min stack O(1) getMin · Valid parentheses matching