[LeetCode] Max Stack 最大栈

Design a max stack that supports push, pop, top, peekMax and popMax.

  1. push(x) -- Push element x onto stack.
  2. pop() -- Remove the element on top of the stack and return it.
  3. top() -- Get the element on the top.
  4. peekMax() -- Retrieve the maximum element in the stack.
  5. popMax() -- Retrieve the maximum element in the stack, and remove it. If you find more than one maximum elements, only remove the top-most one.

Example 1:

MaxStack stack = new MaxStack();
stack.push(5);
stack.push(1);
stack.push(5);
stack.top(); -> 5
stack.popMax(); -> 5
stack.top(); -> 1
stack.peekMax(); -> 5
stack.pop(); -> 1
stack.top(); -> 5

Note:

  1. -1e7 <= x <= 1e7
  2. Number of operations won‘t exceed 10000.
  3. The last four operations won‘t be called when stack is empty.

s

时间: 2024-11-09 04:46:37

[LeetCode] Max Stack 最大栈的相关文章

[CareerCup] 3.2 Min Stack 最小栈

3.2 How would you design a stack which, in addition to push and pop, also has a function min which returns the minimum element? Push, pop and min should all operate in O(1) time. LeetCode上的原题,请参见我之前的博客Min Stack 最小栈.

716. Max Stack - Easy

Design a max stack that supports push, pop, top, peekMax and popMax. push(x) -- Push element x onto stack. pop() -- Remove the element on top of the stack and return it. top() -- Get the element on the top. peekMax() -- Retrieve the maximum element i

heap(堆)和stack(栈)的区别

heap是堆,stack是栈 stack的空间由操作系统自动分配/释放,heap上的空间手动分配/释放. stack空间有限,heap是很大的自由存储区 C中的malloc函数分配的内存空间即在heap上,C++中对应的是new操作符. 程序在编译期对变量和函数分配内存都是在stack(栈)上进行,且程序运行过程中的函数调用时参数的传递也在栈上进行.

[leetcode]Max Points on a Line @ Python

原题地址:https://oj.leetcode.com/problems/max-points-on-a-line/ 题意:Given n points on a 2D plane, find the maximum number of points that lie on the same straight line. 解题思路:找到平面上在一条直线上最多的点.点在同一条直线上意味着这些点的斜率是一样的,那么可以考虑使用哈希表来解决,{斜率:[点1,点2]}这样的映射关系.这里有几个需要考虑

LeetCode: Max Points on a Line [149]

[题目] Given n points on a 2D plane, find the maximum number of points that lie on the same straight line. [题意] 给定一堆点,要求找出一条之前上的最大点数 [思路] 没什么好的方法,从每个点P出发,遍历所有的情况 从每个点P出发,斜率相同的点即为统一之前上的点 注意两种特殊情况: 1. 两个点重合(即为同一个点) 2. 两个点的很坐标相同,即两天构成的之前垂直于X轴 [代码] /** * D

[Twitter] Max Stack

Question: Design a max stack using one stack. ? One stack? I think it is two stacks. class MaxMinStack<T> {   private Stack<T> data;   private Stack<T> max;   private Stack<T> min;   private Comparator<T> comparator;      pub

716. Max Stack

Design a max stack that supports push, pop, top, peekMax and popMax. push(x) -- Push element x onto stack. pop() -- Remove the element on top of the stack and return it. top() -- Get the element on the top. peekMax() -- Retrieve the maximum element i

数据结构与算法之Stack(栈)——in dart

用dart 语言实现一个简单的stack(栈). 1 class Stack<E> { 2 final List<E> _stack; 3 final int capacity; 4 int _top; 5 6 Stack(this.capacity) 7 : _top = -1, 8 _stack = List<E>(capacity); 9 10 bool get isEmpty => _top == -1; 11 bool get isFull =>

[LeetCode] Min Stack 栈

Design a 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 e