Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[Min Stack] The std::deque version passed. Any idea why? #36

Open
ghost opened this issue Nov 26, 2014 · 1 comment
Open

[Min Stack] The std::deque version passed. Any idea why? #36

ghost opened this issue Nov 26, 2014 · 1 comment

Comments

@ghost
Copy link

ghost commented Nov 26, 2014

class MinStack {
public:
    void push(int x) {
        stack_.push_front(x);
        if (minStack_.empty() || x < *minStack_.front()) {
            minStack_.push_front(&stack_.front());
        }
    }
    void pop() {
        if (&stack_.front() == minStack_.front()) {
            minStack_.pop_front();
        }
        stack_.pop_front();
    }
    int top() {
        return stack_.front();
    }
    int getMin() {
        return *minStack_.front();
    }
private:
    deque<int> stack_;
    deque<int*> minStack_;
};

This puzzle really sucks for C++ programers. :-( However the OJ just provide the Java solution.

@ghost
Copy link
Author

ghost commented Dec 1, 2014

I view the source code of the STL (from both clang++ and g++):

std::deque uses a “split buffer” to store the pointers of the new int. And the new capacity is the double of the old one.

It's still confusing.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

0 participants