Skip to content

Commit 8176b2f

Browse files
fix build
1 parent 46ea5b9 commit 8176b2f

File tree

2 files changed

+16
-49
lines changed

2 files changed

+16
-49
lines changed

src/main/java/com/fishercoder/solutions/_1381.java

Lines changed: 15 additions & 49 deletions
Original file line numberDiff line numberDiff line change
@@ -3,47 +3,6 @@
33
import java.util.ArrayList;
44
import java.util.List;
55

6-
/**
7-
* 1381. Design a Stack With Increment Operation
8-
*
9-
* Design a stack which supports the following operations.
10-
*
11-
* Implement the CustomStack class:
12-
*
13-
* CustomStack(int maxSize) Initializes the object with maxSize which is the maximum number of elements in the stack or do nothing if the stack reached the maxSize.
14-
* void push(int x) Adds x to the top of the stack if the stack hasn't reached the maxSize.
15-
* int pop() Pops and returns the top of stack or -1 if the stack is empty.
16-
* void inc(int k, int val) Increments the bottom k elements of the stack by val. If there are less than k elements in the stack, just increment all the elements in the stack.
17-
*
18-
* Example 1:
19-
*
20-
* Input
21-
* ["CustomStack","push","push","pop","push","push","push","increment","increment","pop","pop","pop","pop"]
22-
* [[3],[1],[2],[],[2],[3],[4],[5,100],[2,100],[],[],[],[]]
23-
* Output
24-
* [null,null,null,2,null,null,null,null,null,103,202,201,-1]
25-
* Explanation
26-
* CustomStack customStack = new CustomStack(3); // Stack is Empty []
27-
* customStack.push(1); // stack becomes [1]
28-
* customStack.push(2); // stack becomes [1, 2]
29-
* customStack.pop(); // return 2 --> Return top of the stack 2, stack becomes [1]
30-
* customStack.push(2); // stack becomes [1, 2]
31-
* customStack.push(3); // stack becomes [1, 2, 3]
32-
* customStack.push(4); // stack still [1, 2, 3], Don't add another elements as size is 4
33-
* customStack.increment(5, 100); // stack becomes [101, 102, 103]
34-
* customStack.increment(2, 100); // stack becomes [201, 202, 103]
35-
* customStack.pop(); // return 103 --> Return top of the stack 103, stack becomes [201, 202]
36-
* customStack.pop(); // return 202 --> Return top of the stack 102, stack becomes [201]
37-
* customStack.pop(); // return 201 --> Return top of the stack 101, stack becomes []
38-
* customStack.pop(); // return -1 --> Stack is empty return -1.
39-
*
40-
* Constraints:
41-
* 1 <= maxSize <= 1000
42-
* 1 <= x <= 1000
43-
* 1 <= k <= 1000
44-
* 0 <= val <= 100
45-
* At most 1000 calls will be made to each method of increment, push and pop each separately.
46-
* */
476
public class _1381 {
487
public static class Solution1 {
498
public static class CustomStack {
@@ -83,36 +42,43 @@ public void increment(int k, int val) {
8342
/**
8443
* Implementation of Stack using Array
8544
*/
86-
public static class Solution2{
45+
public static class Solution2 {
8746
public static class CustomStack {
8847
private int top;
8948
private int maxSize;
9049
private int stack[];
50+
9151
public CustomStack(int maxSize) {
9252
this.maxSize = maxSize;
9353
this.stack = new int[maxSize];
9454
}
9555

9656
public void push(int x) {
97-
if(top == maxSize) return;
57+
if (top == maxSize) {
58+
return;
59+
}
9860
stack[top] = x;
9961
top++;
10062
}
10163

10264
public int pop() {
103-
if(top == 0)
65+
if (top == 0) {
10466
return -1;
105-
int popValue = stack[top-1];
106-
stack[top-1] = 0;
67+
}
68+
int popValue = stack[top - 1];
69+
stack[top - 1] = 0;
10770
top--;
10871
return popValue;
10972
}
11073

11174
public void increment(int k, int val) {
112-
if(top == 0 || k == 0) return;
113-
for(int i = 0; i<k; i++){
114-
if(i == top)
75+
if (top == 0 || k == 0) {
76+
return;
77+
}
78+
for (int i = 0; i < k; i++) {
79+
if (i == top) {
11580
break;
81+
}
11682
stack[i] += val;
11783
}
11884
}

src/test/java/com/fishercoder/_1381Test.java

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,7 @@ public void test1() {
2525
assertEquals(201, customStack.pop());
2626
assertEquals(-1, customStack.pop());
2727
}
28+
2829
@Test
2930
public void test2() {
3031
customStack2 = new _1381.Solution2.CustomStack(3);

0 commit comments

Comments
 (0)