716. Max Stack实现一个最大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 in the stack.
- 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的更多相关文章
- [leetcode]716. Max Stack 最大栈
Design a max stack that supports push, pop, top, peekMax and popMax. push(x) -- Push element x onto ...
- JDK的一个关于stack的小bug
在一个项目中,使用了一个java.util.Stack,总所周知,栈是先入后出的,那么遍历其中元素的时候,也应该按照这个顺序遍历才对,但是实际情况确不是,以下是测试代码. Stack stack = ...
- 两个stack实现一个queue
package com.hzins.suanfa; import java.util.Stack; /** * 两个stack实现一个queue * @author Administrator * * ...
- 随机获取min和max之间的一个整数
// 随机获取min和max之间的一个整数 const randomNum = (Min, Max) => { let Range = Max - Min; let Rand = Math.ra ...
- 【LeetCode】716. Max Stack 解题报告(C++)
作者: 负雪明烛 id: fuxuemingzhu 个人博客:http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 双栈 日期 题目地址:https://leetcode ...
- 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 ...
- 推荐一个网站Stack Overflow
网站URL:http://stackoverflow.com 我是怎么知道这个网站的呢?其实这个网站非常出名的,相信许多人都知道.如果你不知道,请继续阅读: 一次我在CSDN上面提问,但是想要再问多几 ...
- 开大Stack的一个小技巧
在程序头部添加一行 #pragma comment(linker, "/STACK:16777216") 可有效开大堆栈 实验效果如下: 11330179 2014-08-05 1 ...
- 数据结构与算法之Stack(栈)的应用——用stack实现一个计算器-/bin/calc.dart
计算器的bin/calc.dart 可执行代码: import 'dart:io'; import 'package:data_struct/stack/sample/calculator.dart' ...
随机推荐
- COMBIN14简单应用
目录 案例1 说明 APDL代码 结果 案例2 说明 APDL代码 结果 案例3 说明 APDL代码 结果 参考网址:http://blog.sina.com.cn/s/blog_65936c2301 ...
- json序列化时定制支持datetime类型,和到中文让他保留中文形式
json序列化时,可以处理的数据类型有哪些?如何定制支持datetime类型 自定义时间序列化转换器 import json from json import JSONEncoder from dat ...
- Python switch(多分支选择)的实现
Python 中没有 switch/case 语法,如果使用 if/elif/else 会出现代码过长.不清晰等问题. 而借助字典就可以实现 switch 的功能 示例: def case1(): # ...
- python3中的编码
python2字符串编码存在的问题: 使用 ASCII 码作为默认编码方式,对中文处理不友好 把字符串分为 unicode 和 str 两种类型,将unicode作为唯一内码,误导开发者 python ...
- Linux之正则表达式1
正则表达式是对字符串操作的一种逻辑公式,就是用事先定义好的一些特定字符.及这些特定字符的组合,组成一个“规则字符串”,这个“规则字符串”用来表达对字符串的一种过滤逻辑.适当使用正则表达式可以提高工作效 ...
- PCL数据类型和ROS数据类型的转换
参考网址 http://wiki.ros.org/pcl/Overview ,重点参看第2和第3节. 1. Data types 介绍了三种点云数据类型:sensor_msgs::PointCloud ...
- oracle表被锁(delete或update一直处于执行状态)的处理办法。
--首先查看有哪些锁 select /*+ rule */ s.username, decode(l.type,'TM','TABLE LOCK','TX','ROW LOCK',null) lock ...
- ylbtech-协议-网络-安全协议:HTTPS
ylbtech-协议-网络-安全协议:HTTPS HTTPS(全称:Hyper Text Transfer Protocol over Secure Socket Layer 或 Hypertext ...
- 静态初始化块和main方法哪个先被执行?
直接看代码 public class BlockAndMain { public static void main(String[] args) { System.out.println(" ...
- MySQL 之 MHA + ProxySQL + keepalived 实现读写分离,高可用(二)
ProxySQL安装 yum/rpm安装 在github或官网上可以下载rpm包,wiki的Getting start章节有详细介绍. cat <<EOF | tee /etc/yum.r ...