Skip to content

Commit 0f8cda9

Browse files
authored
Add the retrieval of minimum and maximum element from stack at O(1) (#5714)
1 parent 87030af commit 0f8cda9

File tree

4 files changed

+287
-0
lines changed

4 files changed

+287
-0
lines changed
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,74 @@
1+
package com.thealgorithms.stacks;
2+
3+
import java.util.NoSuchElementException;
4+
import java.util.Stack;
5+
6+
/**
7+
* A class that implements a stack that gives the maximum element in O(1) time.
8+
* The mainStack is used to store the all the elements of the stack
9+
* While the maxStack stores the maximum elements
10+
* When we want to get a maximum element, we call the top of the maximum stack
11+
*
12+
* Problem: https://www.baeldung.com/cs/stack-constant-time
13+
*/
14+
public class GreatestElementConstantTime {
15+
private Stack<Integer> mainStack; // initialize a mainStack
16+
private Stack<Integer> maxStack; // initialize a maxStack
17+
18+
/**
19+
* Constructs two empty stacks
20+
*/
21+
public GreatestElementConstantTime() {
22+
mainStack = new Stack<>();
23+
maxStack = new Stack<>();
24+
}
25+
26+
/**
27+
* Pushes an element onto the top of the stack.
28+
* Checks if the element is the maximum or not
29+
* If so, then pushes to the maximum stack
30+
* @param data The element to be pushed onto the stack.
31+
*/
32+
public void push(int data) {
33+
if (mainStack.isEmpty()) {
34+
mainStack.push(data);
35+
maxStack.push(data);
36+
return;
37+
}
38+
39+
mainStack.push(data);
40+
if (data > maxStack.peek()) {
41+
maxStack.push(data);
42+
}
43+
}
44+
45+
/**
46+
* Pops an element from the stack.
47+
* Checks if the element to be popped is the maximum or not
48+
* If so, then pop from the minStack
49+
*
50+
* @throws NoSuchElementException if the stack is empty.
51+
*/
52+
public void pop() {
53+
if (mainStack.isEmpty()) {
54+
throw new NoSuchElementException("Stack is empty");
55+
}
56+
57+
int ele = mainStack.pop();
58+
if (ele == maxStack.peek()) {
59+
maxStack.pop();
60+
}
61+
}
62+
63+
/**
64+
* Returns the maximum element present in the stack
65+
*
66+
* @return The element at the top of the maxStack, or null if the stack is empty.
67+
*/
68+
public Integer getMaximumElement() {
69+
if (maxStack.isEmpty()) {
70+
return null;
71+
}
72+
return maxStack.peek();
73+
}
74+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,74 @@
1+
package com.thealgorithms.stacks;
2+
3+
import java.util.NoSuchElementException;
4+
import java.util.Stack;
5+
6+
/**
7+
* A class that implements a stack that gives the minimum element in O(1) time.
8+
* The mainStack is used to store the all the elements of the stack
9+
* While the minStack stores the minimum elements
10+
* When we want to get a minimum element, we call the top of the minimum stack
11+
*
12+
* Problem: https://www.baeldung.com/cs/stack-constant-time
13+
*/
14+
public class SmallestElementConstantTime {
15+
private Stack<Integer> mainStack; // initialize a mainStack
16+
private Stack<Integer> minStack; // initialize a minStack
17+
18+
/**
19+
* Constructs two empty stacks
20+
*/
21+
public SmallestElementConstantTime() {
22+
mainStack = new Stack<>();
23+
minStack = new Stack<>();
24+
}
25+
26+
/**
27+
* Pushes an element onto the top of the stack.
28+
* Checks if the element is the minimum or not
29+
* If so, then pushes to the minimum stack
30+
* @param data The element to be pushed onto the stack.
31+
*/
32+
public void push(int data) {
33+
if (mainStack.isEmpty()) {
34+
mainStack.push(data);
35+
minStack.push(data);
36+
return;
37+
}
38+
39+
mainStack.push(data);
40+
if (data < minStack.peek()) {
41+
minStack.push(data);
42+
}
43+
}
44+
45+
/**
46+
* Pops an element from the stack.
47+
* Checks if the element to be popped is the minimum or not
48+
* If so, then pop from the minStack
49+
*
50+
* @throws NoSuchElementException if the stack is empty.
51+
*/
52+
public void pop() {
53+
if (mainStack.isEmpty()) {
54+
throw new NoSuchElementException("Stack is empty");
55+
}
56+
57+
int ele = mainStack.pop();
58+
if (ele == minStack.peek()) {
59+
minStack.pop();
60+
}
61+
}
62+
63+
/**
64+
* Returns the minimum element present in the stack
65+
*
66+
* @return The element at the top of the minStack, or null if the stack is empty.
67+
*/
68+
public Integer getMinimumElement() {
69+
if (minStack.isEmpty()) {
70+
return null;
71+
}
72+
return minStack.peek();
73+
}
74+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,70 @@
1+
package com.thealgorithms.stacks;
2+
3+
import static org.junit.jupiter.api.Assertions.assertEquals;
4+
import static org.junit.jupiter.api.Assertions.assertNull;
5+
import static org.junit.jupiter.api.Assertions.assertThrows;
6+
7+
import java.util.NoSuchElementException;
8+
import org.junit.jupiter.api.BeforeEach;
9+
import org.junit.jupiter.api.Test;
10+
11+
public class GreatestElementConstantTimeTest {
12+
13+
private GreatestElementConstantTime constantTime;
14+
15+
@BeforeEach
16+
public void setConstantTime() {
17+
constantTime = new GreatestElementConstantTime();
18+
}
19+
20+
@Test
21+
public void testMaxAtFirst() {
22+
constantTime.push(1);
23+
constantTime.push(10);
24+
constantTime.push(20);
25+
constantTime.push(5);
26+
assertEquals(20, constantTime.getMaximumElement());
27+
}
28+
29+
@Test
30+
public void testMinTwo() {
31+
constantTime.push(5);
32+
constantTime.push(10);
33+
constantTime.push(20);
34+
constantTime.push(1);
35+
assertEquals(20, constantTime.getMaximumElement());
36+
constantTime.pop();
37+
constantTime.pop();
38+
assertEquals(10, constantTime.getMaximumElement());
39+
}
40+
41+
@Test
42+
public void testNullMax() {
43+
constantTime.push(10);
44+
constantTime.push(20);
45+
constantTime.pop();
46+
constantTime.pop();
47+
assertNull(constantTime.getMaximumElement());
48+
}
49+
50+
@Test
51+
public void testBlankHandle() {
52+
constantTime.push(10);
53+
constantTime.push(1);
54+
constantTime.pop();
55+
constantTime.pop();
56+
assertThrows(NoSuchElementException.class, () -> constantTime.pop());
57+
}
58+
59+
@Test
60+
public void testPushPopAfterEmpty() {
61+
constantTime.push(10);
62+
constantTime.push(1);
63+
constantTime.pop();
64+
constantTime.pop();
65+
constantTime.push(5);
66+
assertEquals(5, constantTime.getMaximumElement());
67+
constantTime.push(1);
68+
assertEquals(5, constantTime.getMaximumElement());
69+
}
70+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,69 @@
1+
package com.thealgorithms.stacks;
2+
3+
import static org.junit.jupiter.api.Assertions.assertEquals;
4+
import static org.junit.jupiter.api.Assertions.assertNull;
5+
import static org.junit.jupiter.api.Assertions.assertThrows;
6+
7+
import java.util.NoSuchElementException;
8+
import org.junit.jupiter.api.BeforeEach;
9+
import org.junit.jupiter.api.Test;
10+
11+
public class SmallestElementConstantTimeTest {
12+
13+
private SmallestElementConstantTime sect;
14+
15+
@BeforeEach
16+
public void setSect() {
17+
sect = new SmallestElementConstantTime();
18+
}
19+
20+
@Test
21+
public void testMinAtFirst() {
22+
sect.push(1);
23+
sect.push(10);
24+
sect.push(20);
25+
sect.push(5);
26+
assertEquals(1, sect.getMinimumElement());
27+
}
28+
29+
@Test
30+
public void testMinTwo() {
31+
sect.push(5);
32+
sect.push(10);
33+
sect.push(20);
34+
sect.push(1);
35+
assertEquals(1, sect.getMinimumElement());
36+
sect.pop();
37+
assertEquals(5, sect.getMinimumElement());
38+
}
39+
40+
@Test
41+
public void testNullMin() {
42+
sect.push(10);
43+
sect.push(20);
44+
sect.pop();
45+
sect.pop();
46+
assertNull(sect.getMinimumElement());
47+
}
48+
49+
@Test
50+
public void testBlankHandle() {
51+
sect.push(10);
52+
sect.push(1);
53+
sect.pop();
54+
sect.pop();
55+
assertThrows(NoSuchElementException.class, () -> sect.pop());
56+
}
57+
58+
@Test
59+
public void testPushPopAfterEmpty() {
60+
sect.push(10);
61+
sect.push(1);
62+
sect.pop();
63+
sect.pop();
64+
sect.push(5);
65+
assertEquals(5, sect.getMinimumElement());
66+
sect.push(1);
67+
assertEquals(1, sect.getMinimumElement());
68+
}
69+
}

0 commit comments

Comments
 (0)