博客
关于我
【Leetcode】716. Max Stack
阅读量:226 次
发布时间:2019-02-28

本文共 1929 字,大约阅读时间需要 6 分钟。

为了实现一个支持进栈、出栈、查看栈顶、查看最大值以及删掉最大值的数据结构,我们可以使用两个栈的方法。具体来说,一个栈用于存储原始数据,另一个栈用于记录当前栈中的最大值。这种方法不仅能够高效地处理最大值的弹出操作,还能在弹出最大值时保持其他数据的正确性。

核心思路

  • 数据存储

    • 栈s1用于存储原始数据,按照FILO原则进行操作。
    • 栈s2用于存储当前栈中的最大值,保持单调递减的顺序。
  • 操作处理

    • push:将数据推入s1,同时检查并更新s2,确保s2记录当前最大值。
    • pop:从s1中弹出数据,同步处理s2中的最大值记录。
    • top:返回s1的栈顶。
    • peekMax:返回s2的栈顶,即当前最大值。
    • popMax:弹出当前最大值,并调整s1和s2中的数据,确保数据正确性。
  • 时间复杂度与空间复杂度

    • 时间复杂度:除了弹出最大值的操作外,其他操作的时间复杂度为O(1),弹出最大值的时间复杂度为O(n)。
    • 空间复杂度:使用两个栈存储数据,因此空间复杂度为O(n)。
  • 详细步骤

  • 初始化

    • 创建两个栈s1和s2。
  • push(int x)

    • 将数据推入s1。
    • 检查s2,若s2为空或s2的栈顶小于等于当前数据,则推入s2。
  • pop()

    • 弹出s1的栈顶数据x。
    • 检查s2,如果弹出的x等于s2的栈顶,则同时弹出s2的栈顶。
  • top()

    • 返回s1的栈顶。
  • peekMax()

    • 返回s2的栈顶,即当前最大值。
  • popMax()

    • 弹出s2的栈顶,记录为maxValue。
    • 创建临时栈tempStk。
    • 从s1中弹出所有小于maxValue的数据,直到s1为空或栈顶大于等于maxValue。
    • 弹出s1的栈顶。
    • 将tempStk中的数据重新推入s1,同时调用push方法,确保s2记录正确。
  • 代码实现

    import java.util.ArrayDeque;import java.util.Deque;public class MaxStack {    private Deque
    s1; private Deque
    s2; public MaxStack() { s1 = new ArrayDeque<>(); s2 = new ArrayDeque<>(); } public void push(int x) { s1.push(x); if (s2.isEmpty() || s2.peek() <= x) { s2.push(x); } } public int pop() { int x = s1.pop(); if (!s2.isEmpty() && x == s2.peek()) { s2.pop(); } return x; } public int top() { return s1.peek(); } public int peekMax() { return s2.peek(); } public int popMax() { int maxValue = s2.pop(); Deque
    tempStk = new ArrayDeque<>(); while (!s1.isEmpty() && s1.peek() < maxValue) { tempStk.push(s1.pop()); } s1.pop(); while (!tempStk.isEmpty()) { push(tempStk.pop()); } return maxValue; }}

    代码解释

    • push(int x):将数据推入s1,并根据s2的当前状态更新最大值记录。
    • pop():弹出s1的栈顶数据,并同步调整s2中的最大值记录。
    • top():返回s1的栈顶数据。
    • peekMax():返回s2的栈顶,即当前最大值。
    • popMax():弹出当前最大值,并将较小的数据重新推入s1,同时更新s2的记录,确保数据的正确性。

    这种方法通过两个栈的协同工作,确保了数据操作的高效性和正确性,充分利用了栈的先进后出特性,有效地解决了最大值弹出问题。

    转载地址:http://dsds.baihongyu.com/

    你可能感兴趣的文章
    npm和package.json那些不为常人所知的小秘密
    查看>>
    npm和yarn清理缓存命令
    查看>>
    npm和yarn的使用对比
    查看>>
    npm如何清空缓存并重新打包?
    查看>>
    npm学习(十一)之package-lock.json
    查看>>
    npm安装 出现 npm ERR! code ETIMEDOUT npm ERR! syscall connect npm ERR! errno ETIMEDOUT npm ERR! 解决方法
    查看>>
    npm安装crypto-js 如何安装crypto-js, python爬虫安装加解密插件 找不到模块crypto-js python报错解决丢失crypto-js模块
    查看>>
    npm安装教程
    查看>>
    npm报错Cannot find module ‘webpack‘ Require stack
    查看>>
    npm报错Failed at the node-sass@4.14.1 postinstall script
    查看>>
    npm报错fatal: Could not read from remote repository
    查看>>
    npm报错File to import not found or unreadable: @/assets/styles/global.scss.
    查看>>
    npm报错TypeError: this.getOptions is not a function
    查看>>
    npm报错unable to access ‘https://github.com/sohee-lee7/Squire.git/‘
    查看>>
    npm淘宝镜像过期npm ERR! request to https://registry.npm.taobao.org/vuex failed, reason: certificate has ex
    查看>>
    npm版本过高问题
    查看>>
    npm的“--force“和“--legacy-peer-deps“参数
    查看>>
    npm的安装和更新---npm工作笔记002
    查看>>
    npm的常用操作---npm工作笔记003
    查看>>
    npm的常用配置项---npm工作笔记004
    查看>>