Skip to content

Commit 826a96b

Browse files
committed
155
1 parent 42d04fc commit 826a96b

File tree

3 files changed

+83
-0
lines changed

3 files changed

+83
-0
lines changed

Diff for: README.md

+6
Original file line numberDiff line numberDiff line change
@@ -54,3 +54,9 @@ Success is like pregnancy, Everybody congratulates you but nobody knows how many
5454
## 4/17 Tasks (Stacks and Queues: CtCi Chapter 3)
5555
| # | Title | Solution | Time | Space | Difficulty |Tag| Note|
5656
|-----|-------| -------- | ---- | ------|------------|---|-----|
57+
58+
59+
60+
61+
## 4/18 Tasks (Stacks Easy)
62+
|155|[Min Stack](https://leetcode.com/problems/min-stack/#/description)| [Python](./stack_queue/minStack.py) | _O(1)_| _O(n)_ | Easy | ||

Diff for: stack_queue/minStack.py

+48
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,48 @@
1+
#!/usr/bin/python
2+
# -*- coding: utf-8 -*-
3+
4+
# Author: Yu Zhou
5+
6+
# ****************
7+
# 155. Min Stack
8+
# Time: O(1)
9+
# Space: O(n) using extra stack
10+
11+
# Descrption:
12+
# Design a stack that supports push, pop, top, and retrieving the minimum element in constant time.
13+
#
14+
# push(x) -- Push element x onto stack.
15+
# pop() -- Removes the element on top of the stack.
16+
# top() -- Get the top element.
17+
# getMin() -- Retrieve the minimum element in the stack.
18+
# ****************
19+
20+
# 思路:
21+
# 创建一个额外的stack用来储存所有的最小值
22+
# 返回Min的时候,就返回minstack的最后一个值就好了
23+
24+
25+
# ****************
26+
# Final Solution *
27+
# ****************
28+
class MinStack(object):
29+
30+
def __init__(self):
31+
self.stack = []
32+
self.minstack = []
33+
34+
def push(self, x):
35+
self.stack.append(x)
36+
if self.minstack:
37+
x = min(self.minstack[-1], x)
38+
self.minstack.append(x)
39+
40+
def pop(self):
41+
self.minstack.pop()
42+
self.stack.pop()
43+
44+
def top(self):
45+
return self.stack[-1]
46+
47+
def getMin(self):
48+
return self.minstack[-1]

Diff for: stack_queue/template.py

+29
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
#!/usr/bin/python
2+
# -*- coding: utf-8 -*-
3+
4+
# Author: Yu Zhou
5+
6+
# ****************
7+
# Descrption:
8+
# Design a stack that supports push, pop, top, and retrieving the minimum element in constant time.
9+
#
10+
# push(x) -- Push element x onto stack.
11+
# pop() -- Removes the element on top of the stack.
12+
# top() -- Get the top element.
13+
# getMin() -- Retrieve the minimum element in the stack.
14+
# ****************
15+
16+
# 思路:
17+
# 创建一个额外的stack用来储存所有的最小值
18+
# 返回Min的时候,就返回minstack的最后一个值就好了
19+
20+
21+
22+
# ****************
23+
# Final Solution *
24+
# ****************
25+
26+
27+
# ***************************************
28+
# The following code is an fail attempt *
29+
# ***************************************

0 commit comments

Comments
 (0)