Skip to content

Commit 38bf582

Browse files
committed
Time: 140 ms (5.17%), Space: 29.7 MB (10.33%) - LeetHub
1 parent b2b3c5f commit 38bf582

File tree

1 file changed

+55
-0
lines changed

1 file changed

+55
-0
lines changed
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,55 @@
1+
class CustomStack {
2+
public:
3+
int limit;
4+
stack<int> s;
5+
stack<int> temp;
6+
CustomStack(int maxSize) {
7+
limit = maxSize;
8+
}
9+
10+
void push(int x) {
11+
if(s.size() < limit){
12+
s.push(x);
13+
}
14+
}
15+
16+
int pop() {
17+
if(s.empty()){
18+
return -1;
19+
}
20+
int top = s.top();
21+
s.pop();
22+
return top;
23+
}
24+
25+
void increment(int k, int val) {
26+
while(!temp.empty()){
27+
temp.pop();
28+
}
29+
while(!s.empty()){
30+
temp.push(s.top());
31+
s.pop();
32+
}
33+
int v;
34+
while(!temp.empty() && k--){
35+
v = temp.top();
36+
temp.pop();
37+
s.push(v + val);
38+
}
39+
while(!temp.empty()){
40+
s.push(temp.top());
41+
temp.pop();
42+
}
43+
}
44+
};
45+
46+
// Runtime: 76 ms, faster than 8.00% of C++ online submissions for Design a Stack With Increment Operation.
47+
// Memory Usage: 29.7 MB, less than 7.62% of C++ online submissions for Design a Stack With Increment Operation.
48+
49+
/**
50+
* Your CustomStack object will be instantiated and called as such:
51+
* CustomStack* obj = new CustomStack(maxSize);
52+
* obj->push(x);
53+
* int param_2 = obj->pop();
54+
* obj->increment(k,val);
55+
*/

0 commit comments

Comments
 (0)