Stack Advanced¶
Table of Contents¶
- 3170. Lexicographically Minimum String After Removing Stars (Medium)
- 155. Min Stack (Medium)
- 1381. Design a Stack With Increment Operation (Medium)
- 636. Exclusive Time of Functions (Medium)
- 2434. Using a Robot to Print the Lexicographically Smallest String (Medium)
- 895. Maximum Frequency Stack (Hard)
- 1172. Dinner Plate Stacks (Hard)
- 2589. Minimum Time to Complete All Tasks (Hard)
- 716. Max Stack (Hard) 👑
3170. Lexicographically Minimum String After Removing Stars¶
-
LeetCode | LeetCode CH (Medium)
-
Tags: hash table, string, stack, greedy, heap priority queue
155. Min Stack¶
-
LeetCode | LeetCode CH (Medium)
-
Tags: stack, design
- Implement a stack that supports push, pop, top, and retrieving the minimum element in constant time.
155. Min Stack - Python Solution
# Stack
class MinStack:
def __init__(self):
self.stack = []
def push(self, val: int) -> None:
if self.stack:
self.stack.append((val, min(val, self.getMin())))
else:
self.stack.append((val, val))
def pop(self) -> None:
self.stack.pop()
def top(self) -> int:
return self.stack[-1][0]
def getMin(self) -> int:
return self.stack[-1][1]
obj = MinStack()
obj.push(3)
obj.push(2)
obj.pop()
print(obj.top()) # 3
print(obj.getMin()) # 3
155. Min Stack - C++ Solution
#include <algorithm>
#include <climits>
#include <iostream>
#include <stack>
#include <utility>
using namespace std;
class MinStack {
stack<pair<int, int>> st;
public:
MinStack() { st.emplace(0, INT_MAX); }
void push(int val) { st.emplace(val, min(getMin(), val)); }
void pop() { st.pop(); }
int top() { return st.top().first; }
int getMin() { return st.top().second; }
};
int main() {
MinStack minStack;
minStack.push(-2);
minStack.push(0);
minStack.push(-3);
cout << minStack.getMin() << endl; // -3
minStack.pop();
cout << minStack.top() << endl; // 0
cout << minStack.getMin() << endl; // -2
return 0;
}
1381. Design a Stack With Increment Operation¶
-
LeetCode | LeetCode CH (Medium)
-
Tags: array, stack, design
636. Exclusive Time of Functions¶
-
LeetCode | LeetCode CH (Medium)
-
Tags: array, stack
2434. Using a Robot to Print the Lexicographically Smallest String¶
-
LeetCode | LeetCode CH (Medium)
-
Tags: hash table, string, stack, greedy
895. Maximum Frequency Stack¶
-
LeetCode | LeetCode CH (Hard)
-
Tags: hash table, stack, design, ordered set
1172. Dinner Plate Stacks¶
-
LeetCode | LeetCode CH (Hard)
-
Tags: hash table, stack, design, heap priority queue
2589. Minimum Time to Complete All Tasks¶
-
LeetCode | LeetCode CH (Hard)
-
Tags: array, binary search, stack, greedy, sorting
716. Max Stack¶
-
LeetCode | LeetCode CH (Hard)
-
Tags: linked list, stack, design, doubly linked list, ordered set