To the Top
File:  root - text - article - 2019 - 10 - maximum-in-a-stack.txt
Tags: 每日算法题, 算法, 数据结构, 面试题, Daily Problem, Data Structures and Algorithms, | English | Home Page | Category: Computing | 369 Views, 14887 Search Bots | 105 Words

Subscribe to Feed Burner | 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.


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
Tags: 每日算法题, 算法, 数据结构, 面试题, Daily Problem, Data Structures and Algorithms, | English | Home Page | Cateogry: Computing | 369 Views, 14887 Search Bots | 105 Words Subscribe to Feed Burner

Related Articles

  1. Daily Interview Problem: Buddy Strings
  2. Staying on a Chess Board
  3. Windows Scripting
  4. Daily Interview Problem: Min Stack
  5. Algorithm Interview Question: Max and Min with Limited Comparisons
  6. Spectrum Master
  7. Daily Interview Problem: Circle of Chained Words
  8. Daily Interview Puzzle: Ways to Traverse a Grid
  9. [Daily Problem] Course Prerequisites
  10. Daily Interview Problem: Given two arrays, write a function to compute their intersection.

Comments (0)

Your Email (Domain Part Not Exposed):

Your Comments:

Privately By Mail Colors More Smileys S x y @

Verification (Click Image 2 Refresh):

    Be the first one to comment this page !


Page Edited: October 30 2020 14:21:09 | RSS Subscription
How to Cook a Perfect Steak? | <meta name="robots" content="noindex, follow" />