顶部方法的最小堆栈解决方案错误?

时间:2020-07-22 05:43:31

标签: javascript data-structures

Leetcode problem

我的输入输出

["MinStack","push","push","push","getMin","pop","top","getMin"]
[[],[-2],[0],[-3],[],[],[],[]] 

[null,null,null,null,-3,null,-3,-2]

预期是

[null,null,null,null,-3,null,0,-2]

我期望"top"方法是我的问题所在,但是我无法弄清楚自己在做什么错。有人可以为我指出吗?我正在使用两个堆栈来解决问题。通过将其值与第一个堆栈的值进行比较,将一个堆栈压入输入,并将一个minStack压入输入。

/**
 * initialize your data structure here.
 */
let MinStack = function() {
  this.stack = new Stack();
  this.minStack = new Stack();
};

/** 
 * @param {number} x
 * @return {void}
 */
MinStack.prototype.push = function(x) {
  this.stack.push(x);
  if (this.minStack.size === 0) {
    this.minStack.push(x);
  } else if (x <= this.minStack.peek()) {
    this.minStack.push(x);
  }
};

/**
 * @return {void}
 */
MinStack.prototype.pop = function() {
  let popped = this.stack.peek();
  if (popped === this.minStack.peek()) {
    this.minStack.pop()
  }
};

/**
 * @return {number}
 */ 
MinStack.prototype.top = function() {
  return this.stack.peek();
};

/**
 * @return {number}
 */
MinStack.prototype.getMin = function() {
  return this.minStack.peek();
};

class Stack {
  constructor() {
    this.storage = {};
    this.size = 0;
  }
  push(val) {
    this.storage[this.size] = val;
    this.size++;
  }
  pop() {
    let top = this.storage[this.size - 1];
    delete this.storage[this.size - 1];
    this.size--;
    return top;
  }
  peek() {
    return this.storage[this.size - 1];
  }
  getSize() {
    return this.size;
  }
  empty() {
    return this.size === 0;
  }
}

1 个答案:

答案 0 :(得分:0)

好问题!

不确定您的错误,我们可以使用JavaScript数组解决此问题。这样就可以了:


var MinStack = function() {
    this.stack = [];
};

MinStack.prototype.push = function(x) {
    this.stack.push({
        value: x,
        min: this.stack.length === 0 ? x : Math.min(x, this.getMin()),
    });
};


MinStack.prototype.pop = function() {
    this.stack.pop();
};

MinStack.prototype.top = function() {
    return this.stack[this.stack.length - 1].value;
};

MinStack.prototype.getMin = function() {
    return this.stack[this.stack.length - 1].min;
};

如果您想使用自己的Stack(),请将其插入上面的代码中:

this.stack = [];

,它应该可以正常工作。为此,您将不得不编写一个min()方法,我想它会像这样:

class MyOwnMinStack {
    constructor() {
        this.storage = {};
        this.size = 0;
        this.currMin = null
    }
    push(val) {
        this.storage[this.size] = val;
        this.size++;
    }
    pop() {
        let top = this.storage[this.size - 1];
        delete this.storage[this.size - 1];
        this.size--;
        return top;
    }
    peek() {
        return this.storage[this.size - 1];
    }
    getSize() {
        return this.size;
    }
    empty() {
        return this.size === 0;
    }
    min() {
        // To do
        return this.currMin;
    }
}

每次我们push()pop()时,都会跟踪this.currMin,以便一旦{{ 1}}。


在这里,我将复制其他方法来解决其他语言的问题,可能会帮助您解决问题。例如,此方法使用两个堆栈:

O(1)

Python

min()

Java

class MinStack {
private:
    stack<int> stack_one;
    stack<int> stack_two;

public:
    void push(int x) {
        stack_one.push(x);
        if (stack_two.empty() || x <= getMin())
            stack_two.push(x);
    }

    void pop() {
        if (stack_one.top() == getMin())
            stack_two.pop();

        stack_one.pop();
    }

    int top() {
        return stack_one.top();
    }

    int getMin() {
        return stack_two.top();
    }
};


参考文献