Force buckle - minimum stack (Java implementation)

Posted by psycovic23 on Sun, 30 Jan 2022 13:16:12 +0100

Topic content

Design a stack that supports push, pop and top operations and can retrieve the smallest element in a constant time.

  • push(x) -- push element x onto the stack.
  • pop() -- delete the element at the top of the stack.
  • top() -- get the stack top element.
  • getMin() -- retrieves the smallest element in the stack.

Example:

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

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

Explanation:
MinStack minStack = new MinStack();
minStack.push(-2);
minStack.push(0);
minStack.push(-3);
minStack.getMin();   --> return -3.
minStack.pop();
minStack.top();      --> Return 0.
minStack.getMin();   --> return -2.

Tips:

  • pop, top, and getMin operations are always called on non empty stacks.

Problem solving ideas

When solving this problem, the idea is to use an auxiliary stack to store the smallest value in the current stack. When adding the first element, the stack storing the minimum value must be empty, and the first element added at this time must be the current smallest element, so the current element is pushed into the minimum stack. Next, when other elements are put into the stack, they are compared with the top element in the stack where the minimum value is currently stored. If the value of the element to be put into the stack is less than the top element, the element will be pushed into the auxiliary stack where the minimum value is stored at the same time. Similarly, when an element is out of the stack, it should also be compared with the element storing the minimum value. If the value of the element to be out of the stack is equal to the top element of the current auxiliary stack, the top elements in the auxiliary stack will be out of the stack together.

Problem solving code

class MinStack {
    Stack<Integer> stack;
    Stack<Integer> minStack;

    public MinStack() {
        // Two stacks are created when the constructor method is called
        stack = new Stack<>();
        // Create an auxiliary stack to store the minimum value
        minStack = new Stack<>();
    }
    
    public void push(int val) {
        // When entering the stack, it should be noted that if the current element is the smallest element, it will be added to the auxiliary stack additionally
        // First, compare the element to be put into the stack with the element in the current auxiliary stack
        if (minStack.isEmpty() || minStack.peek() >= val) {
            minStack.push(val);
        }
        stack.push(val);
    }
    
    public void pop() {
        // When taking out the elements, first compare them with the elements in the auxiliary stack. If the values are consistent, they will be out of the stack together
        // To compare using equals
        if (stack.pop().equals(minStack.peek())) {
            minStack.pop();
        }
    }
    
    public int top() {
        // Just return the top pointer of the stack
        return stack.peek();
    }
    
    public int getMin() {
        // Directly return the stack top pointer of the smallest stack
        return minStack.peek();
    }
}

/**
 * Your MinStack object will be instantiated and called as such:
 * MinStack obj = new MinStack();
 * obj.push(val);
 * obj.pop();
 * int param_3 = obj.top();
 * int param_4 = obj.getMin();
 */

Topic harvest

In this problem, there is no obstacle in thinking and code logic, but there is a problem in the comparison of element values, resulting in an error in the solution. The problem is that when comparing Integer type data with Integer type data in Java, I should use the equals() method for comparison, and I first used the = = symbol for comparison, resulting in an error. Record and summarize here, and record the comparison between = = and equals() method to strengthen understanding and memory.

Summary record

In Java, Integer type is the wrapper class of int type data, because in jdk1 After 5, it has a new feature of automatic unpacking. Therefore, when comparing Integer type data with int type data, Integer type data will undergo an automatic unpacking process. Therefore, it is essentially equivalent to comparing two int type data, and there are the following two situations when comparing two Integer type data

  • When two are not through new Integer(), but through Integer num = 100; When the Integer type data is obtained in this way, if the Integer type data value is in the range of - 128 ~ 127, because the number of this range will be cached when the Integer type data is used as a constant, the comparison in this case is equivalent to comparing the same address, For numbers beyond this range, it is equivalent to obtaining a new object through new Integer().
  • In any case, as long as it is compared with the Integer type data obtained through new Integer(), it is equivalent to comparing two different objects, so it will not be the same

Therefore, the equality judgment between packaging types should use equals instead of '= =', which is recorded here as a warning!

Topics: Java Algorithm leetcode