|
3 | 3 | import java.util.ArrayList;
|
4 | 4 | import java.util.List;
|
5 | 5 |
|
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 |
| - * */ |
47 | 6 | public class _1381 {
|
48 | 7 | public static class Solution1 {
|
49 | 8 | public static class CustomStack {
|
@@ -83,36 +42,43 @@ public void increment(int k, int val) {
|
83 | 42 | /**
|
84 | 43 | * Implementation of Stack using Array
|
85 | 44 | */
|
86 |
| - public static class Solution2{ |
| 45 | + public static class Solution2 { |
87 | 46 | public static class CustomStack {
|
88 | 47 | private int top;
|
89 | 48 | private int maxSize;
|
90 | 49 | private int stack[];
|
| 50 | + |
91 | 51 | public CustomStack(int maxSize) {
|
92 | 52 | this.maxSize = maxSize;
|
93 | 53 | this.stack = new int[maxSize];
|
94 | 54 | }
|
95 | 55 |
|
96 | 56 | public void push(int x) {
|
97 |
| - if(top == maxSize) return; |
| 57 | + if (top == maxSize) { |
| 58 | + return; |
| 59 | + } |
98 | 60 | stack[top] = x;
|
99 | 61 | top++;
|
100 | 62 | }
|
101 | 63 |
|
102 | 64 | public int pop() {
|
103 |
| - if(top == 0) |
| 65 | + if (top == 0) { |
104 | 66 | 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; |
107 | 70 | top--;
|
108 | 71 | return popValue;
|
109 | 72 | }
|
110 | 73 |
|
111 | 74 | 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) { |
115 | 80 | break;
|
| 81 | + } |
116 | 82 | stack[i] += val;
|
117 | 83 | }
|
118 | 84 | }
|
|
0 commit comments