Design a Stack With Increment Operation LeetCode solution in C++

1
class CustomStack {
2
int *data;
3
int nextIndex;
4
int capacity;
5

6
public:
7
CustomStack(int maxSize) {
8
data = new int[maxSize];
9
nextIndex = 0;
10
capacity = maxSize;
11
}
12

13
void push(int x) {
14
if (nextIndex == capacity) {
15
return;
16
}
17
data[nextIndex] = x;
18
nextIndex++;
19
}
20

21
int pop() {
22
if (nextIndex == 0) {
23
return -1;
24
}
25
int temp = data[nextIndex - 1];
26
nextIndex--;
27
return temp;
28
}
29

30
void increment(int k, int val) {
31
// loop will run upto nextIndex if k >= nextIndex else runt upto k only
32
int n = (k >= nextIndex) ? nextIndex : k;
33
for (int i = 0; i < n; i++) {
34
data[i] = data[i] + val;
35
}
36
}
37
};

0

WPM •0 •0

100%

ACC •0 •0

0s

TIME •0