|
3 | 3 | importjava.util.ArrayList; |
4 | 4 | importjava.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 | publicclass_1381 { |
48 | 7 | publicstaticclassSolution1 { |
49 | 8 | publicstaticclassCustomStack { |
@@ -83,36 +42,43 @@ public void increment(int k, int val) { |
83 | 42 | /** |
84 | 43 | * Implementation of Stack using Array |
85 | 44 | */ |
86 | | -publicstaticclassSolution2{ |
| 45 | +publicstaticclassSolution2{ |
87 | 46 | publicstaticclassCustomStack { |
88 | 47 | privateinttop; |
89 | 48 | privateintmaxSize; |
90 | 49 | privateintstack[]; |
| 50 | + |
91 | 51 | publicCustomStack(intmaxSize) { |
92 | 52 | this.maxSize =maxSize; |
93 | 53 | this.stack =newint[maxSize]; |
94 | 54 | } |
95 | 55 |
|
96 | 56 | publicvoidpush(intx) { |
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 | publicintpop() { |
103 | | -if(top ==0) |
| 65 | +if(top ==0) { |
104 | 66 | return -1; |
105 | | -intpopValue =stack[top-1]; |
106 | | -stack[top-1] =0; |
| 67 | + } |
| 68 | +intpopValue =stack[top -1]; |
| 69 | +stack[top -1] =0; |
107 | 70 | top--; |
108 | 71 | returnpopValue; |
109 | 72 | } |
110 | 73 |
|
111 | 74 | publicvoidincrement(intk,intval) { |
112 | | -if(top ==0 ||k ==0)return; |
113 | | -for(inti =0;i<k;i++){ |
114 | | -if(i ==top) |
| 75 | +if (top ==0 ||k ==0) { |
| 76 | +return; |
| 77 | + } |
| 78 | +for (inti =0;i <k;i++) { |
| 79 | +if (i ==top) { |
115 | 80 | break; |
| 81 | + } |
116 | 82 | stack[i] +=val; |
117 | 83 | } |
118 | 84 | } |
|