Skip to content

Create 1381. Design a Stack With Increment #602

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Oct 2, 2024
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
37 changes: 37 additions & 0 deletions 1381. Design a Stack With Increment
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
class CustomStack {
public:
int k;
vector<int> v;
CustomStack(int maxSize) {
k = maxSize;
}

void push(int x) {
if(v.size()<k){
v.push_back(x);
}
}

int pop() {
if(v.size()>0){
int x = v[v.size()-1];
v.pop_back();
return x;
}
else return -1;
}

void increment(int t, int val) {
for(int i=0; i<min((int)v.size(), t); i++){
v[i]+=val;
}
}
};

/**
* 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);
*/
Loading