• 单调栈


    单调栈,如名字一样,栈内的元素是单调递增或者单调递减的。

    接下来我们用 LeetCode 的题目 155. Min Stack 来说明这种特殊的数据结构,题目说明如下:

    Design a stack that supports push, pop, top, and retrieving the minimum element in constant time.

    • push(x) -- Push element x onto stack.
    • pop() -- Removes the element on top of the stack.
    • top() -- Get the top element.
    • getMin() -- Retrieve the minimum element in the stack.

    Constraints: Methods pop, top and getMin operations will always be called on non-empty stacks.

    显然,在这到题目中,我们需要维持一个单调递减的栈,这样我们才能在 constant time 内获取到最小元素。

    这道题目的思路,就是我们需维护两个栈,栈 stack 保存原来的元素,栈 minStack 保存入栈元素的单调性,在我们新入栈元素的时候,只要新元素比栈 minStack 内的元素小我们就可以入栈。

    这里需要特别注意,我们不要把栈 minStack 内元素出栈。因为栈的特性就是在一端操作。

    /**
     * @param {number} x
     * @return {void}
     */
    MinStack.prototype.push = function (x) {
      this.stack.push(x);
    
      // 这一步操作是有问题的,栈minStack与栈stack的元素序号乱了
      // while (this.minStack.length && this.minStack[this.minStack.length - 1] > x) {
      //  this.minStack.pop();
      // }
      // this.minStack.push(x);
    
      // 新入栈元素小的,即可入栈
      if (!this.minStack.length || x <= this.minStack[this.minStack.length - 1]) {
        this.minStack.push(x);
      }
    };
    

    在我们出栈时候,需要同步把 minStack 内元素也做出栈检查。

    /**
     * @return {void}
     */
    MinStack.prototype.pop = function () {
      let top = this.stack.pop();
      if (top === this.minStack[this.minStack.length - 1]) this.minStack.pop();
    };
    

    单调栈的完整代码如下:

    /**
     * initialize your data structure here.
     */
    const MinStack = function () {
      this.stack = [];
      this.minStack = [];
    };
    
    /**
     * @param {number} x
     * @return {void}
     */
    MinStack.prototype.push = function (x) {
      this.stack.push(x);
      // while (this.minStack.length && this.minStack[this.minStack.length - 1] > x) {
      //  this.minStack.pop();
      // }
      // this.minStack.push(x);
      if (!this.minStack.length || x <= this.minStack[this.minStack.length - 1]) {
        this.minStack.push(x);
      }
    };
    
    /**
     * @return {void}
     */
    MinStack.prototype.pop = function () {
      let top = this.stack.pop();
      if (top === this.minStack[this.minStack.length - 1]) this.minStack.pop();
    };
    
    /**
     * @return {number}
     */
    MinStack.prototype.top = function () {
      return this.stack[this.stack.length - 1];
    };
    
    /**
     * @return {number}
     */
    MinStack.prototype.getMin = function () {
      if (this.minStack.length) {
        return this.minStack[this.minStack.length - 1];
      }
      return this.stack[this.stack.length - 1];
    };
    
  • 相关阅读:
    Java开发桌面程序学习(12)——Javafx 悬浮窗提示 tooptip
    Java开发桌面程序学习(11)——javafx 鼠标点击,右击,双击
    Web前端—— JQuery迷你版实现以及使用
    Web前端——表单提交和Js添加选项
    Web前端——JavaScript练习
    Web前端——JavaScript笔记
    <亲测>CentOS7中使用yum安装Nginx的方法
    Linux 软件安装到 /usr,/usr/local/ 还是 /opt 目录?
    <亲测>centos安装 .net core 2.1
    <亲测>CentOS7 安装mysql8.0(YUM方式)
  • 原文地址:https://www.cnblogs.com/AuKing/p/13984120.html
Copyright © 2020-2023  润新知