-
Notifications
You must be signed in to change notification settings - Fork 12
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Create 1381. Design a Stack With Increment
- Loading branch information
1 parent
9a2b735
commit 663f050
Showing
1 changed file
with
37 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,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); | ||
*/ |