[抄题]:

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 Example--defer

    package main import ( "fmt" "os" ) func main() { f := createFile("/tmp/defe ...

  2. LG1337 [JSOI2004]平衡点 / 吊打XXX

    题意 题目描述 如图:有n个重物,每个重物系在一条足够长的绳子上.每条绳子自上而下穿过桌面上的洞,然后系在一起.图中X处就是公共的绳结.假设绳子是完全弹性的(不会造成能量损失),桌子足够高(因而重物不 ...

  3. CentOS 7 快速部署 ELK

    先简单说一下部署思路及原理(不一定正确,只是个人理解而已) 1.修改系统参数,以便安装软件 2.安装配置elasticsearch,这个相当于总监测中心,用来收集logstash监测的各种服务应用日志 ...

  4. MySQL Error--The Table is full

    问题描述 在MySQL 错误日志中发下以下错误信息:[ERROR] /export/servers/mysql/bin/mysqld: The table '#sql-xxxx-xxx' is ful ...

  5. lsof一些使用

    查看某进程和哪些文件相关 [root@linux-node2 ~]# netstat -lntp Active Internet connections (only servers) Proto Re ...

  6. python制作模块

    自己写的函数,为了下一次方便用,做成模块 主要有这几个步骤: 1:准备发布 2:构建发布 3:导入模块并使用 1:准备发布 首先,我自己写的一个打印出列表(含嵌套列表),打印出列表中的每个数据项,文件 ...

  7. HTTP各种特性

    一.Http客户端 1.浏览器.打开百度首页 2.Curl工具 二.CORS 跨域(浏览器的功能) 1.修改Server.js const http = require('http'); const ...

  8. Spark资源配置(核数与内存)

    转载自:http://blog.csdn.net/zrc199021/article/details/54020692 关于所在节点核数怎么看? =========================== ...

  9. 织梦Dedecms文件目录结构

    Dedecms文件目录结构 /根目录 ../a 默认生成文件存放目录 ../data 系统缓存或其他可写入数据存放目录 ../dede 默认后台登录管理(可任意改名) ../images 系统默认的部 ...

  10. .net 连接 Oracle 可能需要配置

    D:\Program Files (x86)\Oracle Developer Tools for VS2013\network\admin\tnsnames.ora