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' ...
随机推荐
- 使用FileZilla连接时超时,无法连接到服务器
更改一下加密方式,就是不用TLS,用相对不安全方式的(可选项) 腾讯云就是这样的,
- git bash的安装与配置
作业要求来自于:https://edu.cnblogs.com/campus/gzcc/GZCC-16SE2/homework/2097 1.下载安装配置用户名和邮箱. (1)下载安装Github配置 ...
- python 基本语句
python 基本语句 在使用python的变量前必须给它赋值,因为python变量没有默认值. 获取用户输入值 此时需要注意:input函数的返回值为文本或字符串. 一些简单的函数 乘方 绝对值 将 ...
- Azure VMSS (1) 入门
<Windows Azure Platform 系列文章目录> 在使用云计算服务的时候,我们经常需要有自动横向扩展的功能.比如: 1.在业务高峰期,根据负载的增加,自动打开若干台VM 2. ...
- 基础 - 获得CPU主频
// 获得cpu主频.cpp : 定义控制台应用程序的入口点. // #include "stdafx.h" #include <windows.h> #include ...
- Jobs深入学习
代码回顾 Quartz 需要了解你可能希望该作业的实例拥有的各种属性,这是通过JobDetail 类完成的. JobDetail 实例是使用 JobBuilder 类构建的. JobDetail j ...
- Node学习笔记(一)
1. node的特点: Node.js 不是一种独立的语言,与 PHP.Python.Perl.Ruby 的“既是语言也是平台”不同.Node.js 也不是一个 JavaScript 框架,不同于 C ...
- (转)适用微信小程序的table表格(带隔行变色)
原文地址 table.wxml <view class="table"> <view class="tr bg-w"> <view ...
- 3.1链表----链表(Linked List)入门
在分析链表之前,我们先来对之前的动态数组.栈.队列总结一下: (1)底层依托于静态数组 (2)依靠resize解决固定容量问题 (3)是一种假的的动态数据结构 1.什么是链表 可以从以下两个部分来理解 ...
- 《从零玩转python+人工智能-3》网易云课堂王顺子
#1.145——152节课25章——面向对象三大特性小案例 class Animal: def __init__(self,name,age=1): self.name = name self.age ...