-
Notifications
You must be signed in to change notification settings - Fork 5
/
StackWithIncrementOperation.java
51 lines (43 loc) · 1.12 KB
/
StackWithIncrementOperation.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
/*https://leetcode.com/problems/design-a-stack-with-increment-operation/*/
class CustomStack {
private int[] stack;
private int top;
public CustomStack(int maxSize) {
this.top = -1;
this.stack = new int[maxSize];
}
public void push(int x) {
++this.top;
//if index exceeds capacity
if (top == this.stack.length)
{
//skip the current element
top = stack.length-1;
return;
}
//add if there is space in stack
stack[top] = x;
}
public int pop() {
//standard pop operation
if (top == -1) return -1;
return stack[top--];
}
public void increment(int k, int val) {
if (top == -1) return;
//if k is greater than the stack size
if (top+1 <= k)
{
//increment all the elements
for (int i = 0; i <= top; ++i)
stack[i] += val;
}
//otherwise
else
{
//increment the bottom k elements
for (int i = 0; i < k; ++i)
stack[i] += val;
}
}
}