716. Max Stack - Easy

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.

基本思路同 153. Min Stack,用两个stack

注意popMax()的时候要先把stack中的元素pop出来,直到找到max,然后弹出max并把之前弹出的元素重新入栈。stack与maxStack始终同步操作

time: popMax() - O(n), others - O(1), space: O(n)

class MaxStack {
    LinkedList<Integer> stack;
    LinkedList<Integer> maxStack;

    /** initialize your data structure here. */
    public MaxStack() {
        stack = new LinkedList<>();
        maxStack = new LinkedList<>();
    }

    public void push(int x) {
        if(maxStack.isEmpty()) {
            maxStack.offerFirst(x);
        } else {
            maxStack.offerFirst(Math.max(x, maxStack.peekFirst()));
        }
        stack.offerFirst(x);
    }

    public int pop() {
        maxStack.pollFirst();
        return stack.pollFirst();
    }

    public int top() {
        return stack.peekFirst();
    }

    public int peekMax() {
        return maxStack.peekFirst();
    }

    public int popMax() {
        int max = maxStack.peekFirst();
        LinkedList<Integer> tmp = new LinkedList<>();
        while(stack.peekFirst() != max) {
            tmp.offerFirst(stack.pollFirst());
            maxStack.pollFirst();
        }
        stack.pollFirst();
        maxStack.pollFirst();
        while(!tmp.isEmpty()) {
            push(tmp.pollFirst());
        }
        return max;
    }
}

/**
 * Your MaxStack object will be instantiated and called as such:
 * MaxStack obj = new MaxStack();
 * obj.push(x);
 * int param_2 = obj.pop();
 * int param_3 = obj.top();
 * int param_4 = obj.peekMax();
 * int param_5 = obj.popMax();
 */

原文地址:https://www.cnblogs.com/fatttcat/p/10229123.html

时间: 2024-10-08 17:20:38

716. Max Stack - Easy的相关文章

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

[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

[LeetCode] 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

LeetCode Problems List 题目汇总

No. Title Level Rate 1 Two Sum Medium 17.70% 2 Add Two Numbers Medium 21.10% 3 Longest Substring Without Repeating Characters Medium 20.60% 4 Median of Two Sorted Arrays Hard 17.40% 5 Longest Palindromic Substring Medium 20.70% 6 ZigZag Conversion Ea

ELK stack 学习记录

ELK日志分析平台学习记录 首先ELK主要指elasticsearch .logstash 和kibana,三个开源软件组合而成的一套日志平台解决方案.可以将平时收集到的日志,通过前台展示出来,并且可以加以分析,理论上可以解放劳动力(再也不用干上生产取日志这种活了--很搓). 最近在研究ELKstack日志分析平台,网上相关的中文资料不多.所以呢也就写了这篇文章将自己的一些学习认识总结记录下来,基本偏实战,概念理论较少,概念这块,我想以后可以再开一篇文章来做一个阐述总结. 这篇文章中会先讲一下搭

Google interview question: count bounded slices(min/max queue)

Question: A Slice of an array said to be a Bounded slice if Max(SliceArray)-Min(SliceArray)<=K. If Array [3,5,6,7,3] and K=2 provided .. the number of bounded slice is 9, first slice (0,0) in the array Min(0,0)=3 Max(0,0)=3 Max-Min<=K result 0<=2

stack(顺序、链式及应用)

(java版) 1. 顺序栈的实现 顺序栈实现1 [java] package lang; import java.io.Serializable; import java.util.Arrays; /** * @ClassName: ArrayStack * @Description: 顺序栈 * @date 2014年1月20日 上午8:47:19 * @param <T> */ public class ArrayStack<T> implements Serializabl

ftrace的使用【转】

转自:http://blog.csdn.net/cybertan/article/details/8258394 This article explains how to set up ftrace and be able to understand how to trace functions. It should be useful for current kernel developers and device driver developers who want to debug ker

ftrace的使用

This article explains how to set up ftrace and be able to understand how to trace functions. It should be useful for current kernel developers and device driver developers who want to debug kernel issues, and also for students who are keen to pursue