diff --git a/Stack/minStack.cpp b/Stack/minStack.cpp new file mode 100644 index 00000000..0b0bc01b --- /dev/null +++ b/Stack/minStack.cpp @@ -0,0 +1,24 @@ +#include +using namespace std; + +// Min Stack Implementation that supports getMin() in O(1) time and O(1) extra space + +class MinStack { +public: + vector< pair > s; + + MinStack() { } + + void push(int val) { + if(s.empty()) + s.push_back({val,val}); + else + s.push_back({val,min(s.back().second,val)}); + } + + void pop() { s.pop_back(); } + + int top() { return s.back().first; } + + int getMin() { return s.back().second; } +}; \ No newline at end of file diff --git a/Stack/min__Stack.cpp b/Stack/min__Stack.cpp new file mode 100644 index 00000000..0b0bc01b --- /dev/null +++ b/Stack/min__Stack.cpp @@ -0,0 +1,24 @@ +#include +using namespace std; + +// Min Stack Implementation that supports getMin() in O(1) time and O(1) extra space + +class MinStack { +public: + vector< pair > s; + + MinStack() { } + + void push(int val) { + if(s.empty()) + s.push_back({val,val}); + else + s.push_back({val,min(s.back().second,val)}); + } + + void pop() { s.pop_back(); } + + int top() { return s.back().first; } + + int getMin() { return s.back().second; } +}; \ No newline at end of file diff --git a/Stack/min_stack.cpp b/Stack/min_stack.cpp new file mode 100644 index 00000000..0b0bc01b --- /dev/null +++ b/Stack/min_stack.cpp @@ -0,0 +1,24 @@ +#include +using namespace std; + +// Min Stack Implementation that supports getMin() in O(1) time and O(1) extra space + +class MinStack { +public: + vector< pair > s; + + MinStack() { } + + void push(int val) { + if(s.empty()) + s.push_back({val,val}); + else + s.push_back({val,min(s.back().second,val)}); + } + + void pop() { s.pop_back(); } + + int top() { return s.back().first; } + + int getMin() { return s.back().second; } +}; \ No newline at end of file