clueless coding // TODO: be smarter

LeetCode Min Stack: CPP Solution



TL;DR Code Solution


stack<int> mins;
stack<int> stac;

/** initialize your data structure here. */
MinStack() {

}

void push(int x) {
    stac.push(x);
    if (mins.empty() || x < mins.top()) {
        mins.push(x);
    }
    else {
        mins.push(mins.top());
    }
}

void pop() {
    mins.pop();
    stac.pop();
}

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

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