Recca Chao 的 gitHub page

推廣網站開發,包含 Laravel 和 Kotlin 後端撰寫、自動化測試、讀書心得等。Taiwan Kotlin User Group 管理員。

View on GitHub

Hi, here’s your problem today. This problem was recently asked by Uber:

Design a simple stack that supports push, pop, top, and retrieving the minimum element in constant time.

push(x) – Push element x onto stack. pop() – Removes the element on top of the stack. top() – Get the top element. getMin() – Retrieve the minimum element in the stack.

Note: be sure that pop() and top() handle being called on an empty stack.

class minStack(object): def init(self): # Fill this in.

def push(self, x): # Fill this in.

def pop(self): # Fill this in.

def top(self): # Fill this in.

def getMin(self): # Fill this in.

x = minStack() x.push(-2) x.push(0) x.push(-3) print(x.getMin())

-3

x.pop() print(x.top())

0

print(x.getMin())

-2