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 | 425 Views, 16503 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 | 425 Views, 16503 Search Bots | 105 Words Subscribe to Feed Burner

Related Articles

  1. Daily Interview Problem: Merge K Sorted Linked Lists
  2. [Daily Problem] Add two numbers as a linked list
  3. Algorithm Interview: Lowest Common Ancestor of 2 Nodes in Binary Tree
  4. Reverse a Linked List
  5. New Vulnerabilities (CVE-2016-4581) have been detected in CentOS/RHEL/CloudLinux 7
  6. Daily Interview Problem: Find the k-th Largest Element in a List
  7. Daily Interview Puzzle: Minimum Size Subarray Sum
  8. [Daily Problem] Remove k-th Last Element From Linked List
  9. Daily Interview Question: Longest Sequence with Two Unique Numbers
  10. Floor and Ceiling of a Binary Search Tree

Comments (0)

    Be the first one to comment this page !


Page Edited: May 11 2024 14:36:49 | RSS Subscription
How to Cook a Perfect Steak? | <meta name="robots" content="noindex, follow" />