[抄题]:

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. LeetCode - Fruit Into Baskets

    In a row of trees, the i-th tree produces fruit with type tree[i]. You start at any tree of your cho ...

  2. SQL Server 幻读 的真实案例

    数据库中有表[01_SubjectiveScoreInfo],要实现表中的数据只被查出一次,此表数据量较大,有三四百万数据.表结构也确实不是很合理,无法修改表结构,即使是新增一个字段也会有相当大的修改 ...

  3. 建筑的永恒之道 (C·亚历山大 著)

    永恒之道 建筑或城市只有踏上了永恒之道,才会生机勃勃. 第1章 永恒之道 它是一个唯有我们自己才能带秩序的过程,它不可能被求取,但只要我们顺应它,它便会自然而然地出现. 质 为了探求永恒之道,我们首先 ...

  4. python, Image

    PIL: Python Image Library, python平台的图像处理库,要使用Image首先要从PIL库导入Image: from PIL import Image 如果没有安装PIL的包 ...

  5. Linux 判断进程是否运行

    问题 linux平台 多人开发服务器,有时自己运行一个进程在服务器上,但未知原因导致停止运行了,需要添加一个定时任务,用于监控指定进程是否运行 方法 一个通用的方法,以便使用在不同项目中. 思路:定时 ...

  6. istream不是std的成员

    如果报错信息为:istream不是std的成员,那么有两种可能 1.没有包含iostream库文件 2.#ifndef 和#endif使用错误,致使包含的iostream的头文件没有被主函数包含

  7. 第一章 C#入门(Windows窗体应用程序)(二)

    C#窗体应用程序(二) [案例]设计登录界面,效果如下: [案例实现步骤] 1.新建项目(Windows控制台应用程序 文件→新建→项目:选择“项目类型”为Visual C#,“模板”为Windows ...

  8. [zz]蟑螂蚂蚁蚊子已不住在我家了!这个方法100%见效…

    http://mt.sohu.com/20150324/n410238511.shtml 蚂蚁怕酸,蚊子怕辣,蟑螂怕香.在下给各位提供一个不杀生又能驱赶蚂蚁.蚊子.蟑螂的妙法. 一.蚂蚁怕酸味 家里的 ...

  9. Win10系统无法使用小米手机的远程管理功能

    今天想用电脑往手机传点东西,想到可以用小米手机的远程管理功能. 其实就是手机开了一个ftp服务,在电脑上访问手机ftp.没想到啊,居然出错了: 为啥呢,访问不了?我的电脑上文件和打印机共享都开了的. ...

  10. 黄聪:谷歌验证 (Google Authenticator) 的实现原理是什么?

    著作权归作者所有.商业转载请联系作者获得授权,非商业转载请注明出处.作者:徐小花链接:http://www.zhihu.com/question/20462696/answer/18731073来源: ...