Skip to content

Commit 162e114

Browse files
authored
Merge pull request #11 from xchux/feature/155-min-stack
✨ (solution): Problem 155. Min Stack
2 parents 7bd523f + da66c8e commit 162e114

File tree

3 files changed

+114
-0
lines changed

3 files changed

+114
-0
lines changed

solutions/155. Min Stack/README.md

Lines changed: 73 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,73 @@
1+
---
2+
comments: true
3+
difficulty: medium
4+
# Follow `Topics` tags
5+
tags:
6+
- Stack
7+
- Design
8+
---
9+
10+
# [155. Min Stack](https://leetcode.com/problems/min-stack/description/)
11+
12+
## Description
13+
14+
Create a special type of stack called MinStack that, in addition to the usual stack operations, can also return the minimum element currently in the stack — all in constant time (O(1)).
15+
16+
The MinStack class should support the following operations:
17+
18+
1. `MinStack()`: Initializes a new instance of the stack.
19+
20+
2. `void push(int val)`: Adds the given value `val` to the top of the stack.
21+
22+
3. `void pop()`: Removes the top element from the stack.
23+
24+
4. `int top()`: Returns the element currently at the top of the stack.
25+
26+
5. `int getMin()`: Returns the smallest element in the stack at any given time.
27+
28+
Make sure that all these methods run in constant time.
29+
30+
31+
**Example 1:**
32+
```
33+
Input:
34+
["MinStack","push","push","top","push","getMin","pop","top","getMin"]
35+
[[],[-1],[3],[],[-2],[],[],[],[]]
36+
37+
Output: [null,null,3,null,-2,null,3,-1]
38+
39+
Explanation:
40+
MinStack minStack = new MinStack();
41+
minStack.push(-1);
42+
minStack.push(3);
43+
minStack.top(); // return 3
44+
minStack.push(-2);
45+
minStack.getMin(); // return -2
46+
minStack.pop();
47+
minStack.top(); // return 3
48+
minStack.getMin(); // return -1
49+
```
50+
51+
**Constraints:**
52+
53+
* `-231 <= val <= 231 - 1`
54+
* Methods `pop`, `top` and `getMin` operations will always be called on non-empty stacks.
55+
* At most `3 * 10^4` calls will be made to `push`, `pop`, `top`, and `getMin`.
56+
57+
## Solution
58+
59+
Intuitively remove non-alphanumeric and change others to lowercase.
60+
61+
```java
62+
```
63+
64+
```python
65+
```
66+
67+
## Complexity
68+
69+
- Time complexity: $$O(1)$$
70+
<!-- Add time complexity here, e.g. $$O(n)$$ -->
71+
72+
- Space complexity: $$O(n)$$
73+
<!-- Add space complexity here, e.g. $$O(n)$$ -->
Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
import java.util.*;
2+
3+
class Solution {
4+
public boolean isValid(String s) {
5+
Stack<Character> stack = new Stack<>();
6+
for (char c : s.toCharArray()) {
7+
if (c == '(' || c == '{' || c == '[') {
8+
stack.push(c);
9+
} else {
10+
if (stack.isEmpty()) {
11+
return false;
12+
}
13+
char top = stack.pop();
14+
if ((c == ')' && top != '(') || (c == '}' && top != '{') || (c == ']' && top != '[')) {
15+
return false;
16+
}
17+
}
18+
}
19+
return stack.isEmpty();
20+
}
21+
}

solutions/155. Min Stack/Solution.py

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
class MinStack:
2+
3+
def __init__(self):
4+
self.stack = []
5+
6+
def push(self, val: int) -> None:
7+
top = self.stack[-1] if self.stack else -1
8+
min_val = top[1] if isinstance(top, tuple) else float('inf')
9+
if val < min_val:
10+
min_val = val
11+
self.stack.append((val, min_val))
12+
13+
def pop(self) -> None:
14+
self.stack.pop()
15+
16+
def top(self) -> int:
17+
return self.stack[-1][0] if self.stack else None
18+
19+
def getMin(self) -> int:
20+
return self.stack[-1][1] if self.stack else None

0 commit comments

Comments
 (0)