Skip to content

Instantly share code, notes, and snippets.

@Se7soz
Created January 15, 2014 11:38
Show Gist options
  • Select an option

  • Save Se7soz/8434721 to your computer and use it in GitHub Desktop.

Select an option

Save Se7soz/8434721 to your computer and use it in GitHub Desktop.
Read the How to prepare for an interview series at my blog: http://se7so.blogspot.com/2014/01/how-to-prepare-for-interview.html
#include<vector>
#define STACK_IS_EMPTY -1
class CustomStack {
private:
vector<int> stck;
vector<int> mn;
public:
int top() {
if(stck.size() != 0) return stck[stck.size()-1];
throw STACK_IS_EMPTY;
}
void pop() {
if(stck.size() == 0) return;
if(mn[mn.size()-1] == stck[stck.size()-1])
mn.erase(mn.begin()+mn.size()-1);
stck.erase(stck.begin()+stck.size()-1);
}
void push(int n) {
stck.push_back(n);
if(mn.size() == 0 || n <= mn[mn.size()-1])
mn.push_back(n);
}
int size() {
return stck.size();
}
int min() {
if(mn.size() == 0) throw STACK_IS_EMPTY;
return mn[mn.size()-1];
}
bool empty() {
return size() == 0;
}
};
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment