Skip to content

Commit 618dab7

Browse files
add 1381
1 parent c0a2616 commit 618dab7

File tree

3 files changed

+111
-0
lines changed

3 files changed

+111
-0
lines changed

README.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@ _If you like this project, please leave me a star._ ★
88

99
| # | Title | Solutions | Video | Difficulty | Tag
1010
|-----|----------------|---------------|--------|-------------|-------------
11+
|1381|[Design a Stack With Increment Operation](https://leetcode.com/problems/design-a-stack-with-increment-operation/)|[Solution](../master/src/main/java/com/fishercoder/solutions/_1381.java) | |Easy|Stack, Design|
1112
|1380|[Lucky Numbers in a Matrix](https://leetcode.com/problems/lucky-numbers-in-a-matrix/)|[Solution](../master/src/main/java/com/fishercoder/solutions/_1380.java) | |Easy|Array|
1213
|1379|[Find a Corresponding Node of a Binary Tree in a Clone of That Tree](https://leetcode.com/problems/find-a-corresponding-node-of-a-binary-tree-in-a-clone-of-that-tree/)|[Solution](../master/src/main/java/com/fishercoder/solutions/_1379.java) | |Medium|Tree|
1314
|1377|[Frog Position After T Seconds](https://leetcode.com/problems/frog-position-after-t-seconds/)|[Solution](../master/src/main/java/com/fishercoder/solutions/_1377.java) | |Hard|DFS, BFS|
Lines changed: 82 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,82 @@
1+
package com.fishercoder.solutions;
2+
3+
import java.util.ArrayList;
4+
import java.util.List;
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+
public class _1381 {
48+
public static class Solution1 {
49+
public static class CustomStack {
50+
51+
List<Integer> list;
52+
int maxSize;
53+
54+
public CustomStack(int maxSize) {
55+
this.list = new ArrayList<>();
56+
this.maxSize = maxSize;
57+
}
58+
59+
public void push(int x) {
60+
if (list.size() >= maxSize) {
61+
return;
62+
} else {
63+
list.add(x);
64+
}
65+
}
66+
67+
public int pop() {
68+
if (!list.isEmpty()) {
69+
return list.remove(list.size() - 1);
70+
} else {
71+
return -1;
72+
}
73+
}
74+
75+
public void increment(int k, int val) {
76+
for (int i = 0; i < k && i < list.size(); i++) {
77+
list.set(i, list.get(i) + val);
78+
}
79+
}
80+
}
81+
}
82+
}
Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
package com.fishercoder;
2+
3+
import com.fishercoder.solutions._1381;
4+
import org.junit.Test;
5+
6+
import static org.junit.Assert.assertEquals;
7+
8+
public class _1381Test {
9+
private static _1381.Solution1.CustomStack customStack;
10+
11+
@Test
12+
public void test1() {
13+
customStack = new _1381.Solution1.CustomStack(3);
14+
customStack.push(1);
15+
customStack.push(2);
16+
assertEquals(2, customStack.pop());
17+
customStack.push(2);
18+
customStack.push(3);
19+
customStack.push(4);
20+
customStack.increment(5, 100);
21+
customStack.increment(2, 100);
22+
assertEquals(103, customStack.pop());
23+
assertEquals(202, customStack.pop());
24+
assertEquals(201, customStack.pop());
25+
assertEquals(-1, customStack.pop());
26+
}
27+
28+
}

0 commit comments

Comments
 (0)
pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy