- Notifications
You must be signed in to change notification settings - Fork 33
Expand file tree
/
Copy pathDesignAStackWithIncrements.java
More file actions
Latest commit
62 lines (56 loc) · 1.25 KB
/
Copy pathDesignAStackWithIncrements.java
File metadata and controls
62 lines (56 loc) · 1.25 KB
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
52
53
54
55
56
57
58
59
60
61
62
classCustomStack {
intstack[];
intoperations[];
intcapacity;
intsize;
intindex;
publicCustomStack(intmaxSize) {
stack = newint[maxSize];
operations = newint[maxSize];
capacity = maxSize;
size=0;
index = -1;
}
publicvoidpush(intx) {
if(isFull()){
return;
}
index++;
size++;
stack[index] = x;
}
publicintpop() {
if(isEmpty()){
return -1;
}
intval = stack[index];
val += operations[index];
if(index>0){
operations[index-1] += operations[index];
}
operations[index] = 0;
index--;
size--;
returnval;
}
publicvoidincrement(intk, intval) {
if(isEmpty()){
return;
}
intnum = Math.min(size,k);
operations[num-1] += val;
}
privatebooleanisFull(){
return (size == capacity);
}
privatebooleanisEmpty(){
return (size == 0);
}
}
/**
* 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);
*/