File: root - text - article - 2019 - 10 - maximum-in-a-stack.txt
Tags: 每日算法题, 算法, 数据结构, 面试题, Daily Problem, Data Structures and Algorithms, | English | Home Page | Category: Computing | 651 Views, 19560 Search Bots | 105 Words
| Browse | Archive
Tags: 每日算法题, 算法, 数据结构, 面试题, Daily Problem, Data Structures and Algorithms, | English | Home Page | Category: Computing | 651 Views, 19560 Search Bots | 105 Words
| Browse | Archive
Hi, here's your problem today. This problem was recently asked by Twitter:
Implement a class for a stack that supports all the regular functions (push, pop) and an additional function of max() which returns the maximum element in the stack (return None if the stack is empty). Each method should run in constant time.
Tags: 每日算法题, 算法, 数据结构, 面试题, Daily Problem, Data Structures and Algorithms, | English | Home Page | Cateogry: Computing | 651 Views, 19560 Search Bots | 105 Words Implement a class for a stack that supports all the regular functions (push, pop) and an additional function of max() which returns the maximum element in the stack (return None if the stack is empty). Each method should run in constant time.
class MaxStack:
def __init__(self):
# Fill this in.
def push(self, val):
# Fill this in.
def pop(self):
# Fill this in.
def max(self):
# Fill this in.
s = MaxStack()
s.push(1)
s.push(2)
s.push(3)
s.push(2)
print s.max()
# 3
s.pop()
s.pop()
print s.max()
# 2
Related Articles
- Daily Interview Problem: Largest Product of 3 Elements I
- Most Frequent Subtree Sum
- Find Pythagorean Triplets
- Daily Interview Problem: Distribute Bonuses
- Algorithm Interview: Shifted String
- [Daily Problem] Longest Palindromic Substring
- Detect Linked List Cycle
- Sorting a list with 3 unique numbers
- Daily Interview Problem: Circle of Chained Words
- Algorithm Interview: Permutations of numbers
©2006~2024 SteakOverCooked - 0.01087 Seconds(s) - 1724.751 KB/s - 23 Online Memory: 492.15 KB
18:54:01 up 13 days, 18:33, 2 users, load average: 0.98, 0.86, 0.73 - Server PHP Version: 7.4.33
How to Cook a Perfect Steak? | <meta name="robots" content="index, follow">
18:54:01 up 13 days, 18:33, 2 users, load average: 0.98, 0.86, 0.73 - Server PHP Version: 7.4.33
Comments (0)
Read & Write - Normal - Mini - Post - All Comments - Statistics
Be the first one to comment this page !