Skip to content

Commit 36f6504

Browse files
solves min stack in python
1 parent e5cb25a commit 36f6504

File tree

2 files changed

+33
-1
lines changed

2 files changed

+33
-1
lines changed

README.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -43,7 +43,7 @@
4343
| 125 | [Valid Palindrome](https://leetcode.com/problems/valid-palindrome) | Easy | [![Java](https://img.icons8.com/color/40/000000/java-coffee-cup-logo.png)](src/ValidPalindrome.java) [![Python](https://img.icons8.com/color/35/000000/python.png)](python/valid_palindrome.py) |
4444
| 136 | [Single Number](https://leetcode.com/problems/single-number) | Easy | [![Java](https://img.icons8.com/color/40/000000/java-coffee-cup-logo.png)](src/SingleNumber.java) [![Python](https://img.icons8.com/color/35/000000/python.png)](python/single_number.py) |
4545
| 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)](src/LinkedListCycle.java) [![Python](https://img.icons8.com/color/35/000000/python.png)](python/linked_list_cycle.py) |
46-
| 155 | [Min Stack](https://leetcode.com/problems/min-stack) | Easy | [![Java](https://img.icons8.com/color/40/000000/java-coffee-cup-logo.png)](src/MinStack.java) |
46+
| 155 | [Min Stack](https://leetcode.com/problems/min-stack) | Easy | [![Java](https://img.icons8.com/color/40/000000/java-coffee-cup-logo.png)](src/MinStack.java) [![Python](https://img.icons8.com/color/35/000000/python.png)](python/min_stack.py) |
4747
| 157 | [Read N Characters Given Read4](https://leetcode.com/problems/read-n-characters-given-read4) | Easy | |
4848
| 160 | [Intersection of Two Linked Lists](https://leetcode.com/problems/intersection-of-two-linked-lists) | Easy | [![Java](https://img.icons8.com/color/40/000000/java-coffee-cup-logo.png)](src/IntersectionOf2LinkedLists.java) |
4949
| 167 | [Two Sum II - Input Array is Sorted](https://leetcode.com/problems/two-sum-ii-input-array-is-sorted) | Easy | [![Java](https://img.icons8.com/color/40/000000/java-coffee-cup-logo.png)](src/TwoSumIIInputArrayIsSorted.java) |

python/min_stack.py

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
class Node:
2+
def __init__(self, val, min, previous):
3+
self.val = val
4+
self.next = next
5+
self.min = min
6+
self.previous = previous
7+
8+
class MinStack:
9+
10+
def __init__(self):
11+
"""
12+
initialize your data structure here.
13+
"""
14+
self.current = None
15+
16+
def push(self, val: int) -> None:
17+
if self.current is None:
18+
self.current = Node(val, val, None)
19+
else:
20+
self.current.next = Node(val, min(val, self.current.min), self.current)
21+
self.current = self.current.next
22+
23+
def pop(self) -> None:
24+
val = self.current.val
25+
self.current = self.current.previous
26+
return val
27+
28+
def top(self) -> int:
29+
return self.current.val
30+
31+
def getMin(self) -> int:
32+
return self.current.min

0 commit comments

Comments
 (0)