[抄题]:

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

[暴力解法]:

时间分析:

空间分析:

[优化后]:

时间分析:

空间分析:

[奇葩输出条件]:

[奇葩corner case]:

[思维问题]:

知道是用maxstack,但是写不出来

[英文数据结构或算法,为什么不用别的数据结构或算法]:

[一句话思路]:

popmax的暂存值,还要再放回去,不能调用自己,所以再写一个pushHelper函数

[输入量]:空: 正常情况:特大:特小:程序里处理到的特殊情况:异常情况(不合法不合理的输入):

[画图]:

[一刷]:

stack maxstack必须要保持相同的长度,功能就是维持最大值而已

[二刷]:

x不是最大值,就pop到temp中去。此时maxStack也必须要同时pop,才能保持长度时时刻刻一致

[三刷]:

[四刷]:

[五刷]:

[五分钟肉眼debug的结果]:

[总结]:

保持长度时时刻刻一致

[复杂度]:Time complexity: O(n) Space complexity: O(n)

[算法思想:迭代/递归/分治/贪心]:

[关键模板化代码]:

新建的stack,在主函数中直接用名字调用即可

Stack<Integer> stack = new Stack<Integer>();
Stack<Integer> maxStack = new Stack<Integer>(); public MaxStack() {
this.stack = stack;
this.maxStack = maxStack;
}

[其他解法]:

[Follow Up]:

[LC给出的题目变变变]:

[代码风格] :

[是否头一次写此类driver funcion的代码] :

就是新建一个类,然后调用方法就行了

// package whatever; // don’t place package name!

import java.io.*;
import java.util.*;
import java.lang.*; class MaxStack { Stack<Integer> stack = new Stack<Integer>();
Stack<Integer> maxStack = new Stack<Integer>(); public MaxStack() {
this.stack = stack;
this.maxStack = maxStack;
} public void pushHelper(int x) {
//if the max exits
int max = maxStack.isEmpty() ? Integer.MIN_VALUE : maxStack.peek(); //push into 2 stacks
//equal max or not
if (x > max) {
stack.push(x);
maxStack.push(x);
}else {
stack.push(x);
maxStack.push(max);
}
} public void push(int x) {
pushHelper(x);
} public int pop() {
//pop the normal
maxStack.pop();
return stack.pop();
} public int top() {
//return the normal
return stack.peek();
} public int peekMax() {
return maxStack.peek();
} public int popMax() {
//get max
int max = maxStack.peek(); //initialize a temp stack
Stack<Integer> temp = new Stack<Integer>(); //while x < max, store into temp
while (stack.peek() < max) {
int x = stack.pop();
temp.push(x);
//should pop at the same time
maxStack.pop();
} //once equal, pop both
stack.pop();
maxStack.pop(); //retrive by pushHelper
while (!temp.isEmpty()) {
int x = temp.pop();
pushHelper(x);
} //return
return max;
}
} class MyCode {
public static void main (String[] args) {
MaxStack answer = new MaxStack();
answer.push(5);
answer.push(10000);
answer.push(4);
answer.push(100);
int rst = answer.popMax();
System.out.println("rst = " +rst);
}
}

[潜台词] :

716. Max Stack实现一个最大stack的更多相关文章

  1. [leetcode]716. Max Stack 最大栈

    Design a max stack that supports push, pop, top, peekMax and popMax. push(x) -- Push element x onto ...

  2. JDK的一个关于stack的小bug

    在一个项目中,使用了一个java.util.Stack,总所周知,栈是先入后出的,那么遍历其中元素的时候,也应该按照这个顺序遍历才对,但是实际情况确不是,以下是测试代码. Stack stack = ...

  3. 两个stack实现一个queue

    package com.hzins.suanfa; import java.util.Stack; /** * 两个stack实现一个queue * @author Administrator * * ...

  4. 随机获取min和max之间的一个整数

    // 随机获取min和max之间的一个整数 const randomNum = (Min, Max) => { let Range = Max - Min; let Rand = Math.ra ...

  5. 【LeetCode】716. Max Stack 解题报告(C++)

    作者: 负雪明烛 id: fuxuemingzhu 个人博客:http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 双栈 日期 题目地址:https://leetcode ...

  6. 716. Max Stack (follow up questions for min stack)

    Design a max stack that supports push, pop, top, peekMax and popMax. push(x) -- Push element x onto ...

  7. 推荐一个网站Stack Overflow

    网站URL:http://stackoverflow.com 我是怎么知道这个网站的呢?其实这个网站非常出名的,相信许多人都知道.如果你不知道,请继续阅读: 一次我在CSDN上面提问,但是想要再问多几 ...

  8. 开大Stack的一个小技巧

    在程序头部添加一行 #pragma comment(linker, "/STACK:16777216") 可有效开大堆栈 实验效果如下: 11330179 2014-08-05 1 ...

  9. 数据结构与算法之Stack(栈)的应用——用stack实现一个计算器-/bin/calc.dart

    计算器的bin/calc.dart 可执行代码: import 'dart:io'; import 'package:data_struct/stack/sample/calculator.dart' ...

随机推荐

  1. go-json处理的问题

    1.通过Decoder来解析json串 package main import ( "encoding/json" "fmt" "io" & ...

  2. 虚拟机安装及Oracle安装

    1.安装虚拟机(没难度,傻瓜装机) 新建虚拟机 自定义------下一步------- 稍后安装操作系统------下一步 下一步 下一步至完成 然后启动,就可以启动一个系统咯!!! 可以查一下虚拟机 ...

  3. 十五、springcloud(一)注册中心Eureka

    1.Eureka的基本架构 a.Eureka Server 提供服务注册和发现 b.Service Provider 服务提供方 将自身服务注册到Eureka,从而使服务消费方能够找到 c.Servi ...

  4. List进行排序

    使用Java中提供的对集合进行操作的工具类Collections,其中的sort方法,重写比较方法 /** * 根据序号对列席人进行排序 * @param lxrUser * @return */ p ...

  5. Linux服务器同步Intetnet时间

    Linux服务器同步Intetnet时间 在使用linux服务器(虚拟机)的过程中,经常会发现使用一段时间后,linux服务器的时间和我的宿主机的时间不一致,而宿主机的时间确实是internet时间, ...

  6. Excel函数之rank应用

    该函数的功能就是对现有数据指标进行排名 示例:对产品进行销售总额的排名 首先要知道排名需要用到rank函数 number参数就是你要进行排名的数据 ref参数就是该指标需要在哪个区域内进行比较定位排名 ...

  7. Nodepad++ 进行数据分析操作

    查找: ^.*大师兄.*$ 替换为:(空)   如果不留空行: 查找: ^.*大师兄.*\r?\n   注意: Notepad++的[全部替换]受[方向]约束,所以如果想“向下”全部替换,要把光标放到 ...

  8. 分享一个生成反遗忘复习计划的java程序

    想必这个曲线大家都认识,这是遗忘曲线,展示人的记忆会随着时间的延长慢慢遗忘的规律,同时还展示了如果我们过一段时间复习一次对遗忘的有利影响. 道理大家都懂,关键怎么做到? 靠在本子上记下今天我该复习哪一 ...

  9. 彻底删除msde2008(请先在控制面板中卸载).bat

    彻底删除msde2008(请先在控制面板中卸载).bat @echo offset /P dv=请确认强制删除MSDE2008(请先在控制面板中卸载),Y=删除,N=退出:if not defined ...

  10. mmap映射区和shm共享内存的区别总结

    [转载]原文链接:https://blog.csdn.net/hj605635529/article/details/73163513 linux中的两种共享内存.一种是我们的IPC通信System ...