MCPcopy Create free account
hub / github.com/Tiwarishashwat/InterviewCodes / CustomStack

Class CustomStack

DesignAStackWithIncrements.java:1–54  ·  view source on GitHub ↗

Source from the content-addressed store, hash-verified

1class CustomStack {
2 int stack[];
3 int operations[];
4 int capacity;
5 int size;
6 int index;
7 public CustomStack(int maxSize) {
8 stack = new int[maxSize];
9 operations = new int[maxSize];
10 capacity = maxSize;
11 size=0;
12 index = -1;
13 }
14
15 public void push(int x) {
16 if(isFull()){
17 return;
18 }
19 index++;
20 size++;
21 stack[index] = x;
22 }
23
24 public int pop() {
25 if(isEmpty()){
26 return -1;
27 }
28 int val = stack[index];
29 val += operations[index];
30 if(index>0){
31 operations[index-1] += operations[index];
32 }
33 operations[index] = 0;
34 index--;
35 size--;
36 return val;
37 }
38
39 public void increment(int k, int val) {
40 if(isEmpty()){
41 return;
42 }
43 int num = Math.min(size,k);
44 operations[num-1] += val;
45 }
46
47 private boolean isFull(){
48 return (size == capacity);
49 }
50
51 private boolean isEmpty(){
52 return (size == 0);
53 }
54}
55
56/**
57 * Your CustomStack object will be instantiated and called as such:

Callers

nothing calls this directly

Calls

no outgoing calls

Tested by

no test coverage detected