mirror of
https://github.com/TheAlgorithms/Java.git
synced 2025-07-06 00:54:32 +08:00
feat: Add MinStackUsingTwoStacks
new algorithm with Junit tests (#5758)
This commit is contained in:
@ -614,6 +614,7 @@
|
||||
* [LargestRectangle](https://github.com/TheAlgorithms/Java/blob/master/src/main/java/com/thealgorithms/stacks/LargestRectangle.java)
|
||||
* [MaximumMinimumWindow](https://github.com/TheAlgorithms/Java/blob/master/src/main/java/com/thealgorithms/stacks/MaximumMinimumWindow.java)
|
||||
* [MinStackUsingSingleStack](https://github.com/TheAlgorithms/Java/blob/master/src/main/java/com/thealgorithms/stacks/MinStackUsingSingleStack.java)
|
||||
* [MinStackUsingTwoStacks](https://github.com/TheAlgorithms/Java/blob/master/src/main/java/com/thealgorithms/stacks/MinStackUsingTwoStacks.java)
|
||||
* [NextGreaterElement](https://github.com/TheAlgorithms/Java/blob/master/src/main/java/com/thealgorithms/stacks/NextGreaterElement.java)
|
||||
* [NextSmallerElement](https://github.com/TheAlgorithms/Java/blob/master/src/main/java/com/thealgorithms/stacks/NextSmallerElement.java)
|
||||
* [PostfixEvaluator](https://github.com/TheAlgorithms/Java/blob/master/src/main/java/com/thealgorithms/stacks/PostfixEvaluator.java)
|
||||
@ -1168,6 +1169,7 @@
|
||||
* [InfixToPrefixTest](https://github.com/TheAlgorithms/Java/blob/master/src/test/java/com/thealgorithms/stacks/InfixToPrefixTest.java)
|
||||
* [LargestRectangleTest](https://github.com/TheAlgorithms/Java/blob/master/src/test/java/com/thealgorithms/stacks/LargestRectangleTest.java)
|
||||
* [MinStackUsingSingleStackTest](https://github.com/TheAlgorithms/Java/blob/master/src/test/java/com/thealgorithms/stacks/MinStackUsingSingleStackTest.java)
|
||||
* [MinStackUsingTwoStacksTest](https://github.com/TheAlgorithms/Java/blob/master/src/test/java/com/thealgorithms/stacks/MinStackUsingTwoStacksTest.java)
|
||||
* [NextGreaterElementTest](https://github.com/TheAlgorithms/Java/blob/master/src/test/java/com/thealgorithms/stacks/NextGreaterElementTest.java)
|
||||
* [NextSmallerElementTest](https://github.com/TheAlgorithms/Java/blob/master/src/test/java/com/thealgorithms/stacks/NextSmallerElementTest.java)
|
||||
* [PostfixEvaluatorTest](https://github.com/TheAlgorithms/Java/blob/master/src/test/java/com/thealgorithms/stacks/PostfixEvaluatorTest.java)
|
||||
|
@ -0,0 +1,57 @@
|
||||
package com.thealgorithms.stacks;
|
||||
|
||||
import java.util.Stack;
|
||||
|
||||
/**
|
||||
* Min-Stack implementation that supports push, pop, and retrieving the minimum element in constant time.
|
||||
*
|
||||
* @author Hardvan
|
||||
*/
|
||||
public final class MinStackUsingTwoStacks {
|
||||
MinStackUsingTwoStacks() {
|
||||
}
|
||||
|
||||
private final Stack<Integer> stack = new Stack<>();
|
||||
private final Stack<Integer> minStack = new Stack<>();
|
||||
|
||||
/**
|
||||
* Pushes a new element onto the {@code stack}.
|
||||
* If the value is less than or equal to the current minimum, it is also pushed onto the {@code minStack}.
|
||||
*
|
||||
* @param value The value to be pushed.
|
||||
*/
|
||||
public void push(int value) {
|
||||
stack.push(value);
|
||||
if (minStack.isEmpty() || value <= minStack.peek()) {
|
||||
minStack.push(value);
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Removes the top element from the stack.
|
||||
* If the element is the minimum element, it is also removed from the {@code minStack}.
|
||||
*/
|
||||
public void pop() {
|
||||
if (stack.pop().equals(minStack.peek())) {
|
||||
minStack.pop();
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Retrieves the top element of the stack.
|
||||
*
|
||||
* @return The top element.
|
||||
*/
|
||||
public int top() {
|
||||
return stack.peek();
|
||||
}
|
||||
|
||||
/**
|
||||
* Retrieves the minimum element in the stack.
|
||||
*
|
||||
* @return The minimum element.
|
||||
*/
|
||||
public int getMin() {
|
||||
return minStack.peek();
|
||||
}
|
||||
}
|
@ -0,0 +1,38 @@
|
||||
package com.thealgorithms.stacks;
|
||||
|
||||
import static org.junit.jupiter.api.Assertions.assertEquals;
|
||||
|
||||
import org.junit.jupiter.api.Test;
|
||||
|
||||
public class MinStackUsingTwoStacksTest {
|
||||
|
||||
@Test
|
||||
public void testMinStackOperations() {
|
||||
MinStackUsingTwoStacks minStack = new MinStackUsingTwoStacks();
|
||||
minStack.push(3);
|
||||
minStack.push(5);
|
||||
assertEquals(3, minStack.getMin());
|
||||
|
||||
minStack.push(2);
|
||||
minStack.push(1);
|
||||
assertEquals(1, minStack.getMin());
|
||||
|
||||
minStack.pop();
|
||||
assertEquals(2, minStack.getMin());
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testMinStackOperations2() {
|
||||
MinStackUsingTwoStacks minStack = new MinStackUsingTwoStacks();
|
||||
minStack.push(3);
|
||||
minStack.push(5);
|
||||
assertEquals(3, minStack.getMin());
|
||||
|
||||
minStack.push(2);
|
||||
minStack.push(1);
|
||||
assertEquals(1, minStack.getMin());
|
||||
|
||||
minStack.pop();
|
||||
assertEquals(2, minStack.getMin());
|
||||
}
|
||||
}
|
Reference in New Issue
Block a user