Movatterモバイル変換


[0]ホーム

URL:


Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commit8176b2f

Browse files
fix build
1 parent46ea5b9 commit8176b2f

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
importjava.util.ArrayList;
44
importjava.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
publicclass_1381 {
487
publicstaticclassSolution1 {
498
publicstaticclassCustomStack {
@@ -83,36 +42,43 @@ public void increment(int k, int val) {
8342
/**
8443
* Implementation of Stack using Array
8544
*/
86-
publicstaticclassSolution2{
45+
publicstaticclassSolution2{
8746
publicstaticclassCustomStack {
8847
privateinttop;
8948
privateintmaxSize;
9049
privateintstack[];
50+
9151
publicCustomStack(intmaxSize) {
9252
this.maxSize =maxSize;
9353
this.stack =newint[maxSize];
9454
}
9555

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

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

11174
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) {
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
publicvoidtest2() {
3031
customStack2 =new_1381.Solution2.CustomStack(3);

0 commit comments

Comments
 (0)

[8]ページ先頭

©2009-2025 Movatter.jp