From db58a102cfa8bdf2d40a00f07ab07771c6ff6f3c Mon Sep 17 00:00:00 2001 From: Deepak Kumar <137646137+deepakkumar9546@users.noreply.github.com> Date: Sat, 31 Aug 2024 13:51:51 +0530 Subject: [PATCH] Time: 15 ms (77.22%), Space: 22.2 MB (50.69%) - LeetHub --- 0155-min-stack/0155-min-stack.cpp | 29 +++++++++++++++++++++++++++++ 1 file changed, 29 insertions(+) create mode 100644 0155-min-stack/0155-min-stack.cpp diff --git a/0155-min-stack/0155-min-stack.cpp b/0155-min-stack/0155-min-stack.cpp new file mode 100644 index 0000000..25bf7e1 --- /dev/null +++ b/0155-min-stack/0155-min-stack.cpp @@ -0,0 +1,29 @@ +class MinStack { + vector> st; + +public: + MinStack() {} + + void push(int val) { + int min_val = getMin(); + if (st.empty() || min_val > val) { + min_val = val; + } + st.push_back({val, min_val}); + } + + void pop() { st.pop_back(); } + + int top() { return st.empty() ? -1 : st.back()[0]; } + + int getMin() { return st.empty() ? -1 : st.back()[1]; } +}; + +/** + * Your MinStack object will be instantiated and called as such: + * MinStack* obj = new MinStack(); + * obj->push(val); + * obj->pop(); + * int param_3 = obj->top(); + * int param_4 = obj->getMin(); + */ \ No newline at end of file