zoukankan      html  css  js  c++  java
  • 单调栈

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

    接下来我们用 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];
    };
    
  • 相关阅读:
    C# Timer用法及实例详解
    c#使用MethodInvoker解决跨线程访问控件
    C# 网络通信大小端转换类
    c# 高效的线程安全队列ConcurrentQueue(下) Segment类
    OPTIMIZE TABLE
    TRUNCATE TABLE 与 DELETE table 区别
    mysql 优化
    linux 内核---------董昊 ( Robin Dong ) and OenHan
    Linux内核分析:页回收导致的cpu load瞬间飙高的问题分析与思考--------------蘑菇街技术博客
    google perftools分析程序性能
  • 原文地址:https://www.cnblogs.com/AuKing/p/13984120.html
Copyright © 2011-2022 走看看