File tree Expand file tree Collapse file tree 1 file changed +52
-0
lines changed
DesignAStackWithIncrementOperation Expand file tree Collapse file tree 1 file changed +52
-0
lines changed Original file line number Diff line number Diff line change
1
+ class CustomStack {
2
+ private:
3
+ const int m_maxSize;
4
+ vector<int > m_stack;
5
+
6
+ public:
7
+ CustomStack (int maxSize) : m_maxSize(maxSize)
8
+ {
9
+
10
+ }
11
+
12
+ void push (int x)
13
+ {
14
+ if (m_stack.size () < m_maxSize)
15
+ {
16
+ // add element to top of stack (back of vector)
17
+ m_stack.push_back (x);
18
+ }
19
+ }
20
+
21
+ int pop ()
22
+ {
23
+ int val = -1 ; // -1 means stack is empty
24
+
25
+ if (! m_stack.empty ())
26
+ {
27
+ val = m_stack.back ();
28
+
29
+ // remove top element in stack (back of vector)
30
+ m_stack.pop_back ();
31
+ }
32
+
33
+ return val;
34
+ }
35
+
36
+ void increment (int k, int val)
37
+ {
38
+ // increment k elements from the bottom of the stack (start of the vector) by val
39
+ for (int i=0 ; i<min (k, (int )m_stack.size ()); i++)
40
+ {
41
+ m_stack.at (i) += val;
42
+ }
43
+ }
44
+ };
45
+
46
+ /* *
47
+ * Your CustomStack object will be instantiated and called as such:
48
+ * CustomStack* obj = new CustomStack(maxSize);
49
+ * obj->push(x);
50
+ * int param_2 = obj->pop();
51
+ * obj->increment(k,val);
52
+ */
You can’t perform that action at this time.
0 commit comments