Skip to content

Commit 96f74a7

Browse files
solves min stack
1 parent e35b6d7 commit 96f74a7

File tree

2 files changed

+32
-1
lines changed

2 files changed

+32
-1
lines changed

README.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -47,7 +47,7 @@
4747
| 125 | [Valid Palindrome](https://leetcode.com/problems/valid-palindrome) | Easy | [![Java](https://img.icons8.com/color/40/000000/java-coffee-cup-logo.png)](https://github.com/anishLearnsToCode/leetcode-algorithms/blob/master/src/ValidPalindrome.java) |
4848
| 136 | [Single Number](https://leetcode.com/problems/single-number) | Easy | [![Java](https://img.icons8.com/color/40/000000/java-coffee-cup-logo.png)](https://github.com/anishLearnsToCode/leetcode-algorithms/blob/master/src/SingleNumber.java) |
4949
| 141 | [Linked List Cycle](https://leetcode.com/problems/linked-list-cycle) | Easy | [![Java](https://img.icons8.com/color/40/000000/java-coffee-cup-logo.png)](https://github.com/anishLearnsToCode/leetcode-algorithms/blob/master/src/LinkedListCycle.java) |
50-
| 155 | [Min Stack](https://leetcode.com/problems/min-stack) | Easy |
50+
| 155 | [Min Stack](https://leetcode.com/problems/min-stack) | Easy |
5151
| 157 | [Read N Characters Given Read4](https://leetcode.com/problems/read-n-characters-given-read4) | Easy |
5252
| 160 | [Intersection of Two Linked Lists](https://leetcode.com/problems/intersection-of-two-linked-lists) | Easy |
5353
| 167 | [Two Sum II - Input Array is Sorted](https://leetcode.com/problems/two-sum-ii-input-array-is-sorted) | Easy |

src/MinStack.java

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
import java.util.ArrayList;
2+
import java.util.List;
3+
4+
public class MinStack {
5+
6+
private final List<Integer> numbers = new ArrayList<>();
7+
8+
/** initialize your data structure here. */
9+
public MinStack() {
10+
}
11+
12+
public void push(int x) {
13+
numbers.add(x);
14+
}
15+
16+
public void pop() {
17+
numbers.remove(numbers.size() - 1);
18+
}
19+
20+
public int top() {
21+
return numbers.get(numbers.size() - 1);
22+
}
23+
24+
public int getMin() {
25+
int min = Integer.MAX_VALUE;
26+
for (int element : numbers) {
27+
min = Math.min(min, element);
28+
}
29+
return min;
30+
}
31+
}

0 commit comments

Comments
 (0)