File tree 1 file changed +55
-0
lines changed
1381-design-a-stack-with-increment-operation
1 file changed +55
-0
lines changed Original file line number Diff line number Diff line change
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
+ */
You can’t perform that action at this time.
0 commit comments