-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Time: 15 ms (77.22%), Space: 22.2 MB (50.69%) - LeetHub
- Loading branch information
1 parent
382f95b
commit db58a10
Showing
1 changed file
with
29 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,29 @@ | ||
class MinStack { | ||
vector<vector<int>> 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(); | ||
*/ |