Skip to content

Commit

Permalink
Time: 140 ms (5.17%), Space: 29.7 MB (10.33%) - LeetHub
Browse files Browse the repository at this point in the history
  • Loading branch information
ayushnagar123 committed Jan 26, 2022
1 parent b2b3c5f commit 38bf582
Showing 1 changed file with 55 additions and 0 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,55 @@
class CustomStack {
public:
int limit;
stack<int> s;
stack<int> temp;
CustomStack(int maxSize) {
limit = maxSize;
}

void push(int x) {
if(s.size() < limit){
s.push(x);
}
}

int pop() {
if(s.empty()){
return -1;
}
int top = s.top();
s.pop();
return top;
}

void increment(int k, int val) {
while(!temp.empty()){
temp.pop();
}
while(!s.empty()){
temp.push(s.top());
s.pop();
}
int v;
while(!temp.empty() && k--){
v = temp.top();
temp.pop();
s.push(v + val);
}
while(!temp.empty()){
s.push(temp.top());
temp.pop();
}
}
};

// Runtime: 76 ms, faster than 8.00% of C++ online submissions for Design a Stack With Increment Operation.
// Memory Usage: 29.7 MB, less than 7.62% of C++ online submissions for Design a Stack With Increment Operation.

/**
* Your CustomStack object will be instantiated and called as such:
* CustomStack* obj = new CustomStack(maxSize);
* obj->push(x);
* int param_2 = obj->pop();
* obj->increment(k,val);
*/

0 comments on commit 38bf582

Please sign in to comment.