Skip to content

Commit 85c237e

Browse files
committed
added retrieval of minimum and maximum element from stack at O(1)
1 parent 7326ab2 commit 85c237e

File tree

2 files changed

+144
-0
lines changed

2 files changed

+144
-0
lines changed
Lines changed: 72 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,72 @@
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+
public class GreatestElementConstantTime {
13+
private Stack<Integer> mainStack;
14+
private Stack<Integer> maxStack;
15+
16+
/**
17+
* Constructs two empty stacks
18+
*/
19+
public GreatestElementConstantTime() {
20+
mainStack = new Stack<>();
21+
maxStack = new Stack<>();
22+
}
23+
24+
/**
25+
* Pushes an element onto the top of the stack.
26+
* Checks if the element is the maximum or not
27+
* If so, then pushes to the maximum stack
28+
* @param data The element to be pushed onto the stack.
29+
*/
30+
public void push(int data) {
31+
if (mainStack.isEmpty()) {
32+
mainStack.push(data);
33+
maxStack.push(data);
34+
return;
35+
}
36+
37+
mainStack.push(data);
38+
if (data > maxStack.peek()) {
39+
maxStack.push(data);
40+
}
41+
}
42+
43+
/**
44+
* Pops an element from the stack.
45+
* Checks if the element to be popped is the maximum or not
46+
* If so, then pop from the minStack
47+
*
48+
* @throws NoSuchElementException if the stack is empty.
49+
*/
50+
public void pop() {
51+
if (mainStack.isEmpty()) {
52+
throw new NoSuchElementException("Stack is empty");
53+
}
54+
55+
int ele = mainStack.pop();
56+
if (ele == maxStack.peek()) {
57+
maxStack.pop();
58+
}
59+
}
60+
61+
/**
62+
* Returns the maximum element present in the stack
63+
*
64+
* @return The element at the top of the maxStack, or null if the stack is empty.
65+
*/
66+
public Integer getMaximumElement() {
67+
if (maxStack.isEmpty()) {
68+
return null;
69+
}
70+
return maxStack.peek();
71+
}
72+
}
Lines changed: 72 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,72 @@
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+
public class SmallestElementConstantTime {
13+
private Stack<Integer> mainStack;
14+
private Stack<Integer> minStack;
15+
16+
/**
17+
* Constructs two empty stacks
18+
*/
19+
public SmallestElementConstantTime() {
20+
mainStack = new Stack<>();
21+
minStack = new Stack<>();
22+
}
23+
24+
/**
25+
* Pushes an element onto the top of the stack.
26+
* Checks if the element is the minimum or not
27+
* If so, then pushes to the minimum stack
28+
* @param data The element to be pushed onto the stack.
29+
*/
30+
public void push(int data) {
31+
if (mainStack.isEmpty()) {
32+
mainStack.push(data);
33+
minStack.push(data);
34+
return;
35+
}
36+
37+
mainStack.push(data);
38+
if (data < minStack.peek()) {
39+
minStack.push(data);
40+
}
41+
}
42+
43+
/**
44+
* Pops an element from the stack.
45+
* Checks if the element to be popped is the minimum or not
46+
* If so, then pop from the minStack
47+
*
48+
* @throws NoSuchElementException if the stack is empty.
49+
*/
50+
public void pop() {
51+
if (mainStack.isEmpty()) {
52+
throw new NoSuchElementException("Stack is empty");
53+
}
54+
55+
int ele = mainStack.pop();
56+
if (ele == minStack.peek()) {
57+
minStack.pop();
58+
}
59+
}
60+
61+
/**
62+
* Returns the minimum element present in the stack
63+
*
64+
* @return The element at the top of the minStack, or null if the stack is empty.
65+
*/
66+
public Integer getMinimumElement() {
67+
if (minStack.isEmpty()) {
68+
return null;
69+
}
70+
return minStack.peek();
71+
}
72+
}

0 commit comments

Comments
 (0)