博客
关于我
【剑指 Offer 30】js 包含min函数的栈
阅读量:664 次
发布时间:2019-03-15

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

栈(Stack)的数据结构,本质上是一种Last-In-First-Out(LIFO)的线性数据结构。栈的核心操作包括push(压栈)、pop(弹栈)和top(获取栈顶元素)。为了实现栈中的最小值(min)查询,同时确保push、pop和min的时间复杂度均为O(1),我们需要引入一个辅助栈来维护当前栈中最小值的相关信息。

在本文中,我们将通过一个名为MinStack的数据类来实现上述目标。MinStack类包含两个栈:dataStack用来存储所有push进去的数据;minStack则用来存储辅助信息,辅助信息主要是帮助我们快速找到栈中的最小值。

核心思路

我们使用辅助栈(minStack)来维护当前栈中最小值的相关信息。具体来说:

  • push操作

    • 将数据push入dataStack。
    • 如果minStack为空,或者当前数据小于等于minStack的栈顶元素,则将数据也push入minStack。这样可以确保minStack始终保存当前栈中最小的元素。
  • pop操作

    • 如果当前栈的栈顶元素等于minStack的栈顶元素,则删除minStack的栈顶元素。这个操作的目的是为了确保当栈发生变化时,最小值的辅助信息能够及时更新。
  • min操作

    • 最直接的方式就是返回minStack的栈顶元素,因为minStack始终保存栈中最小的元素。
  • 这种方法可以在O(1)的时间复杂度内完成push、pop和min操作。

    代码实现

    以下是MinStack类的完整实现代码:

    var MinStack = function () {    this.dataStack = [];    this.minStack = [];};MinStack.prototype.push = function (x) {    this.dataStack.push(x);    const length = this.minStack.length;    if (length === 0 || x <= this.minStack[length - 1]) {        this.minStack.push(x);    }};MinStack.prototype.pop = function () {    const { minStack, dataStack } = this;    if (minStack[minStack.length - 1] === dataStack[dataStack.length - 1]) {        minStack.pop();    }    dataStack.pop();};MinStack.prototype.top = function () {    const length = this.dataStack.length;    if (!length) {        return null;    } else {        return this.dataStack[length - 1];    }};MinStack.prototype.min = function () {    const length = this.minStack.length;    if (!length) {        return null;    } else {        return this.minStack[length - 1];    }};

    总结

    通过引入辅助栈的方式,我们可以在O(1)的时间复杂度内实现栈的push、pop和min操作。这一设计巧妙地结合了栈的基本操作和最小值查询的需求,确保在处理大数据量时依然能够高效运行。这种设计方法既简洁又高效,是实现高性能栈操作的经典解决方案。

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

    你可能感兴趣的文章
    NMF(非负矩阵分解)
    查看>>
    nmon_x86_64_centos7工具如何使用
    查看>>
    NN&DL4.1 Deep L-layer neural network简介
    查看>>
    NN&DL4.3 Getting your matrix dimensions right
    查看>>
    NN&DL4.8 What does this have to do with the brain?
    查看>>
    nnU-Net 终极指南
    查看>>
    No 'Access-Control-Allow-Origin' header is present on the requested resource.
    查看>>
    NO 157 去掉禅道访问地址中的zentao
    查看>>
    no available service ‘default‘ found, please make sure registry config corre seata
    查看>>
    no connection could be made because the target machine actively refused it.问题解决
    查看>>
    No Datastore Session bound to thread, and configuration does not allow creation of non-transactional
    查看>>
    No fallbackFactory instance of type class com.ruoyi---SpringCloud Alibaba_若依微服务框架改造---工作笔记005
    查看>>
    No Feign Client for loadBalancing defined. Did you forget to include spring-cloud-starter-loadbalanc
    查看>>
    No mapping found for HTTP request with URI [/...] in DispatcherServlet with name ...的解决方法
    查看>>
    No mapping found for HTTP request with URI [/logout.do] in DispatcherServlet with name 'springmvc'
    查看>>
    No module named 'crispy_forms'等使用pycharm开发
    查看>>
    No module named cv2
    查看>>
    No module named tensorboard.main在安装tensorboardX的时候遇到的问题
    查看>>
    No module named ‘MySQLdb‘错误解决No module named ‘MySQLdb‘错误解决
    查看>>
    No new migrations found. Your system is up-to-date.
    查看>>