소스코드
class MinStack {
stack<int> elements;
stack<int> sorted;
public:
/** initialize your data structure here. */
MinStack() {
}
void push(int x) {
elements.push(x);
if(sorted.empty() || sorted.top() >= x) sorted.push(x);
}
void pop() {
if(elements.empty()) return;
if(sorted.top() == elements.top()) sorted.pop();
elements.pop();
}
int top() {
return elements.top();
}
int getMin() {
return sorted.top();
}
};
/**
* Your MinStack object will be instantiated and called as such:
* MinStack* obj = new MinStack();
* obj->push(x);
* obj->pop();
* int param_3 = obj->top();
* int param_4 = obj->getMin();
*/
'개인 공부 > 코딩테스트' 카테고리의 다른 글
[C++][leetcode] Last Stone Weight :: seoftware (0) | 2020.04.13 |
---|---|
[C++][leetcode] Diameter of Binary Tree :: seoftware (0) | 2020.04.12 |
[c++][leetcode] Backspace String Compare :: seoftware (0) | 2020.04.10 |
[c++][백준] 1475번 방 번호 :: seoftware (0) | 2020.04.10 |
[C++][백준] 14502번 연구소 :: seoftware (0) | 2020.04.10 |
댓글