LeetCode //C - 155. Min Stack

155. Min Stack

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

Implement the MinStack class:

  • MinStack() initializes the stack object.
  • void push(int val) pushes the element val onto the stack.
  • void pop() removes the element on the top of the stack.
  • int top() gets the top element of the stack.
  • int getMin() retrieves the minimum element in the stack.

You must implement a solution with O(1) time complexity for each function.
 

Example 1:

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

Constraints:

  • − 2 31 < = v a l < = 2 31 − 1 -2^{31} <= val <= 2^{31} - 1 231<=val<=2311
  • Methods pop, top and getMin operations will always be called on non-empty stacks.
  • At most 3 ∗ 1 0 4 3 * 10^4 3104 calls will be made to push, pop, top, and getMin.

From: LeetCode
Link: 155. Min Stack


Solution:

Ideas:

One way to implement the MinStack with constant time complexity for each function is to maintain two stacks internally:

  1. The main stack: This will store all the pushed elements.
  2. The min stack: This will store the minimum values. The top of this stack will always be the current minimum value of the main stack.

When pushing a new element onto the main stack:

  1. Push the element onto the main stack.
  2. Compare the new element with the top of the min stack. If the new element is less than or equal to the top of the min stack (or if the min stack is empty), push the new element onto the min stack.

When popping an element from the main stack:

  1. Pop the element from the main stack.
  2. If the popped element is equal to the top of the min stack, pop it from the min stack as well.
    top() will simply return the top of the main stack, and getMin() will return the top of the min stack.
Code:
typedef struct {
    
    
    int *data;    // main stack
    int *min;     // min stack
    int topIndex; // current top index
    int capacity; // total capacity of the stack
} MinStack;

/** initialize your data structure here. */
MinStack* minStackCreate() {
    
    
    MinStack *obj = (MinStack*)malloc(sizeof(MinStack));
    obj->capacity = 10000; // initial capacity
    obj->data = (int*)malloc(obj->capacity * sizeof(int));
    obj->min = (int*)malloc(obj->capacity * sizeof(int));
    obj->topIndex = -1; // empty stack
    return obj;
}

void minStackPush(MinStack* obj, int val) {
    
    
    // If stack is full, double its capacity
    if (obj->topIndex == obj->capacity - 1) {
    
    
        obj->capacity *= 2;
        obj->data = (int*)realloc(obj->data, obj->capacity * sizeof(int));
        obj->min = (int*)realloc(obj->min, obj->capacity * sizeof(int));
    }
    // Push to main stack
    obj->data[++obj->topIndex] = val;
    
    // Push to min stack if it's empty or val is less than or equal to current minimum
    if (obj->topIndex == 0 || val <= obj->min[obj->topIndex - 1]) {
    
    
        obj->min[obj->topIndex] = val;
    } else {
    
    
        obj->min[obj->topIndex] = obj->min[obj->topIndex - 1];
    }
}

void minStackPop(MinStack* obj) {
    
    
    if (obj->topIndex >= 0) {
    
    
        obj->topIndex--;
    }
}

int minStackTop(MinStack* obj) {
    
    
    return obj->data[obj->topIndex];
}

int minStackGetMin(MinStack* obj) {
    
    
    return obj->min[obj->topIndex];
}

void minStackFree(MinStack* obj) {
    
    
    free(obj->data);
    free(obj->min);
    free(obj);
}

/**
 * Your MinStack struct will be instantiated and called as such:
 * MinStack* obj = minStackCreate();
 * minStackPush(obj, val);
 * minStackPop(obj);
 * int param_3 = minStackTop(obj);
 * int param_4 = minStackGetMin(obj);
 * minStackFree(obj);
 */

猜你喜欢

转载自blog.csdn.net/navicheung/article/details/132400644