-
Notifications
You must be signed in to change notification settings - Fork 0
/
DesignaStackWithIncrementOperation.java
61 lines (49 loc) · 1.13 KB
/
DesignaStackWithIncrementOperation.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
52
53
54
55
56
57
58
59
60
61
package stack;
import utils.LeetCode;
import utils.Level;
@LeetCode(no=1381,
title = "Design a Stack With Increment Operation",
level = Level.MEDIUM,
url = "https://leetcode.com/problems/design-a-stack-with-increment-operation/")
public class DesignaStackWithIncrementOperation {
}
class CustomStack {
int maxSize;
int arr[];
int curr;
public CustomStack(int maxSize) {
this.maxSize=maxSize;
this.arr= new int[maxSize];
curr=0;
}
public void push(int x) {
if(curr<maxSize){
arr[curr]=x;
curr++;
}
}
public int pop() {
if(curr>0){
int elem= arr[curr-1];
arr[curr-1]=0;
curr--;
return elem;
}else{
return -1;
}
}
public void increment(int k, int val) {
if(k>maxSize)
k=maxSize;
for(int i=0;i<k;i++){
arr[i]+=val;
}
}
}
/**
* 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);
*/