File tree Expand file tree Collapse file tree 3 files changed +97
-0
lines changed
main/java/com/thealgorithms/stacks
test/java/com/thealgorithms/stacks Expand file tree Collapse file tree 3 files changed +97
-0
lines changed Original file line number Diff line number Diff line change 614614 * [ LargestRectangle] ( https://github.com/TheAlgorithms/Java/blob/master/src/main/java/com/thealgorithms/stacks/LargestRectangle.java )
615615 * [ MaximumMinimumWindow] ( https://github.com/TheAlgorithms/Java/blob/master/src/main/java/com/thealgorithms/stacks/MaximumMinimumWindow.java )
616616 * [ MinStackUsingSingleStack] ( https://github.com/TheAlgorithms/Java/blob/master/src/main/java/com/thealgorithms/stacks/MinStackUsingSingleStack.java )
617+ * [ MinStackUsingTwoStacks] ( https://github.com/TheAlgorithms/Java/blob/master/src/main/java/com/thealgorithms/stacks/MinStackUsingTwoStacks.java )
617618 * [ NextGreaterElement] ( https://github.com/TheAlgorithms/Java/blob/master/src/main/java/com/thealgorithms/stacks/NextGreaterElement.java )
618619 * [ NextSmallerElement] ( https://github.com/TheAlgorithms/Java/blob/master/src/main/java/com/thealgorithms/stacks/NextSmallerElement.java )
619620 * [ PostfixEvaluator] ( https://github.com/TheAlgorithms/Java/blob/master/src/main/java/com/thealgorithms/stacks/PostfixEvaluator.java )
11681169 * [ InfixToPrefixTest] ( https://github.com/TheAlgorithms/Java/blob/master/src/test/java/com/thealgorithms/stacks/InfixToPrefixTest.java )
11691170 * [ LargestRectangleTest] ( https://github.com/TheAlgorithms/Java/blob/master/src/test/java/com/thealgorithms/stacks/LargestRectangleTest.java )
11701171 * [ MinStackUsingSingleStackTest] ( https://github.com/TheAlgorithms/Java/blob/master/src/test/java/com/thealgorithms/stacks/MinStackUsingSingleStackTest.java )
1172+ * [ MinStackUsingTwoStacksTest] ( https://github.com/TheAlgorithms/Java/blob/master/src/test/java/com/thealgorithms/stacks/MinStackUsingTwoStacksTest.java )
11711173 * [ NextGreaterElementTest] ( https://github.com/TheAlgorithms/Java/blob/master/src/test/java/com/thealgorithms/stacks/NextGreaterElementTest.java )
11721174 * [ NextSmallerElementTest] ( https://github.com/TheAlgorithms/Java/blob/master/src/test/java/com/thealgorithms/stacks/NextSmallerElementTest.java )
11731175 * [ PostfixEvaluatorTest] ( https://github.com/TheAlgorithms/Java/blob/master/src/test/java/com/thealgorithms/stacks/PostfixEvaluatorTest.java )
Original file line number Diff line number Diff line change 1+ package com .thealgorithms .stacks ;
2+
3+ import java .util .Stack ;
4+
5+ /**
6+ * Min-Stack implementation that supports push, pop, and retrieving the minimum element in constant time.
7+ *
8+ * @author Hardvan
9+ */
10+ public final class MinStackUsingTwoStacks {
11+ MinStackUsingTwoStacks () {
12+ }
13+
14+ private final Stack <Integer > stack = new Stack <>();
15+ private final Stack <Integer > minStack = new Stack <>();
16+
17+ /**
18+ * Pushes a new element onto the {@code stack}.
19+ * If the value is less than or equal to the current minimum, it is also pushed onto the {@code minStack}.
20+ *
21+ * @param value The value to be pushed.
22+ */
23+ public void push (int value ) {
24+ stack .push (value );
25+ if (minStack .isEmpty () || value <= minStack .peek ()) {
26+ minStack .push (value );
27+ }
28+ }
29+
30+ /**
31+ * Removes the top element from the stack.
32+ * If the element is the minimum element, it is also removed from the {@code minStack}.
33+ */
34+ public void pop () {
35+ if (stack .pop ().equals (minStack .peek ())) {
36+ minStack .pop ();
37+ }
38+ }
39+
40+ /**
41+ * Retrieves the top element of the stack.
42+ *
43+ * @return The top element.
44+ */
45+ public int top () {
46+ return stack .peek ();
47+ }
48+
49+ /**
50+ * Retrieves the minimum element in the stack.
51+ *
52+ * @return The minimum element.
53+ */
54+ public int getMin () {
55+ return minStack .peek ();
56+ }
57+ }
Original file line number Diff line number Diff line change 1+ package com .thealgorithms .stacks ;
2+
3+ import static org .junit .jupiter .api .Assertions .assertEquals ;
4+
5+ import org .junit .jupiter .api .Test ;
6+
7+ public class MinStackUsingTwoStacksTest {
8+
9+ @ Test
10+ public void testMinStackOperations () {
11+ MinStackUsingTwoStacks minStack = new MinStackUsingTwoStacks ();
12+ minStack .push (3 );
13+ minStack .push (5 );
14+ assertEquals (3 , minStack .getMin ());
15+
16+ minStack .push (2 );
17+ minStack .push (1 );
18+ assertEquals (1 , minStack .getMin ());
19+
20+ minStack .pop ();
21+ assertEquals (2 , minStack .getMin ());
22+ }
23+
24+ @ Test
25+ public void testMinStackOperations2 () {
26+ MinStackUsingTwoStacks minStack = new MinStackUsingTwoStacks ();
27+ minStack .push (3 );
28+ minStack .push (5 );
29+ assertEquals (3 , minStack .getMin ());
30+
31+ minStack .push (2 );
32+ minStack .push (1 );
33+ assertEquals (1 , minStack .getMin ());
34+
35+ minStack .pop ();
36+ assertEquals (2 , minStack .getMin ());
37+ }
38+ }
You can’t perform that action at this time.
0 commit comments