forked from fishercoder1534/Leetcode
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy path_1381.java
87 lines (75 loc) · 2.18 KB
/
_1381.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
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
package com.fishercoder.solutions;
import java.util.ArrayList;
import java.util.List;
public class _1381 {
public static class Solution1 {
public static class CustomStack {
List<Integer> list;
int maxSize;
public CustomStack(int maxSize) {
this.list = new ArrayList<>();
this.maxSize = maxSize;
}
public void push(int x) {
if (list.size() >= maxSize) {
return;
} else {
list.add(x);
}
}
public int pop() {
if (!list.isEmpty()) {
return list.remove(list.size() - 1);
} else {
return -1;
}
}
public void increment(int k, int val) {
for (int i = 0; i < k && i < list.size(); i++) {
list.set(i, list.get(i) + val);
}
}
}
}
/**
* Implementation of Stack using Array
*/
public static class Solution2 {
public static class CustomStack {
private int top;
private int maxSize;
private int[] stack;
public CustomStack(int maxSize) {
this.maxSize = maxSize;
this.stack = new int[maxSize];
}
public void push(int x) {
if (top == maxSize) {
return;
}
stack[top] = x;
top++;
}
public int pop() {
if (top == 0) {
return -1;
}
int popValue = stack[top - 1];
stack[top - 1] = 0;
top--;
return popValue;
}
public void increment(int k, int val) {
if (top == 0 || k == 0) {
return;
}
for (int i = 0; i < k; i++) {
if (i == top) {
break;
}
stack[i] += val;
}
}
}
}
}