chore(Java): add finding min value on stack (#622)

pull/637/head
Chandana Akriti 2021-11-17 00:14:30 +05:30 committed by GitHub
parent 5d8fbc07b5
commit 25b0c63a8e
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 84 additions and 0 deletions

View File

@ -71,6 +71,7 @@
- [The Stock Span Problem](stacks/the-stock-span-problem.java)
- [Celebrity Problem](stacks/celebrity-problem.java)
- [Sliding Window Maximum](stacks/sliding-window-maximum.java)
- [Min Stack](stacks/Min-Stack.java)
## Strings

View File

@ -0,0 +1,83 @@
// Design a stack that supports push, pop, top, and retrieving the minimum element in constant time.
// Implement the MinStack class:
// MinStack() initializes the stack object.
// void push(int val) pushes the element val onto the stack.
// void pop() removes the element on the top of the stack.
// int top() gets the top element of the stack.
// int getMin() retrieves the minimum element in the stack.
// Java program to implement a stack that supports
// getMinimum() in O(1) time and O(1) extra space.
import java.util.*;
// A user defined stack that supports getMin() in
// addition to push() and pop()
class MinStack {
long min = 0;
Stack<Long> stack = new Stack<>();
public void push(int x) {
// only push the old minimum value when the current
// minimum value changes after pushing the new value x
if(stack.size() == 0){
stack.push((long) x);
min = x;
}
else if(x < min){
stack.push((long) 2*x - min);
min=x;
}else
stack.push((long) x);
}
public void pop() {
// if pop operation could result in the changing of the current minimum value,
// pop twice and change the current minimum value to the last minimum value.
if(stack.peek() < min) {
min = 2*min - stack.pop();
}else
stack.pop();
}
public int top() {
if (stack.peek() < min)
return (int) min;
else
return (int) (long) stack.peek();
}
public int getMin() {
return (int) min;
}
}
// Driver Code
public class Main
{
public static void main(String[] args)
{
MinStack s = new MinStack();
s.push(3);
s.push(5);
System.out.println("Min element present is: "+ s.getMin());
s.push(2);
s.push(1);
System.out.println("Min element present is: "+ s.getMin());
s.pop();
System.out.println("Min element present is: "+ s.getMin());
s.pop();
System.out.println("Top element present is: " + s.top());
}
}
//TC: O(1)
//SC: O(1)
// Output
// Min element present is: 3
// Min element present is: 1
// Min element present is: 2
// Top element present is: 5