Stacks
Stacks
import java.util.*;
class Stack {
private int arr[];
private int top;
System.out.println(st.size());
System.out.println(st.peak());
System.out.println(st.pop());
System.out.println(st.peak());
st.push(23);
while(!st.isEmpty()){
System.out.println(st.pop());
}
}
}
-----------------------------------------------------------------
//Implementation of Stack using LinkedList(Dynamic in Nature)
class Node {
public int data;
public Node next;
class LLStack
{
public Node head;
public int size;
public LLStack()
{
head = null;
size = 0;
}
return res;
}
while(!st.isEmpty()){
System.out.println(st.pop());
}
}
}
-----------------------------------------------------------------------
//Next Greater Element To The Right
public static int[] solve(int[] arr){
int[] nge = new int[arr.length];
Stack<Integer>st = new Stack<>();
st.push(arr[arr.length-1]);
nge[arr.length-1] = -1;
5 3 8 -2 7
8 8 -1 7 -1
-----------------------------------------------------------------
//Next Greater Element To The Left
if(st.size()==0){
nge[i] = -1;
}else{
nge[i] = st.peek();
}
st.push(arr[i]);
}
return nge;
}
--------------------------------------------------------
//Next Smaller Element To The Right
Input: 5 3 8 -2 7
Output: 3 -2 -2 -1 -1
-----------------------------------------------------------
//Next Smaller Element To The Left
Input: 5 3 8 -2 7
Output: -1 -1 3 -1 -2
-----------------------------------------------------------------------
//Next Greater Element I
return ans;
}
query : 4 1 2
arr : 1 3 4 2
nge : 3 4 -1 -1
ans: -1 4 -1
-----------------------------------------------------------------------
//Next Greater Element(circular nums)
return ans;
}
Input: 1 2 1
output: 2 -1 2
-------------------------------------------------------------------------
//Largest Area Histogram 2
return maxArea;
}
input: 2 1 5 6 2 3
output: 10
-----------------------------------------------------------------------
//Validate Stack Sequences
pushed: 1 2 3 4 5
popped: 4 5 3 2 1
ans: true;
--------------------------------------------------------------------------------
//Minimum Add To Make Parentheses Valid
//Stack
input: ()))((
output: 4
------------------------------------------------------------------
//Remove Outermost Parentheses
}else{
st.pop();
if(st.size()>0){
sb.append(ch);
}
}
}
return sb.toString();
}
(()())(())
ans : ()()()
-----------------------------------------------------------------------
//Score Of Parentheses
if(s.charAt(i)=='('){
st.push(-1);
}else{
if(st.peek()==-1){
st.pop();
st.push(1);
}else{
int val = 0;
while(st.peek()!=-1){
val += st.pop();
}
st.pop();
st.push(2*val);
}
}
}
int val = 0;
while(st.size()>0){
val += st.pop();
}
return val;
}
-----------------------------------------------------------
//
System.out.println(false);
}
------------------------------------------------------------------------
//Balanced Brackets
}
public static boolean handleClosing(Stack < Character> st, char corresopch)
{
if (st.size() == 0)
{
return false;
}
else if (st.peek() != corresopch)
{
return false;
}
else
{
st.pop();
return true;
}
}
-----------------------------------------------------------------------------------
----
//Remove K Digits
while(k>0){
st.pop();
k--;
}
int d = 0;
while(d<ans.length && ans[d] == '0') d++;
input: 1432219
k : 3
output: 1219
------------------------------------------------------------------------
//Asteroid Collision
}else{
st.push(val);
}
}
}
return ans;
}
input: 3 2 -1 3 -3 3
output: 1 -4
------------------------------------------------------------------------------
//Trapping Rain Water
int ans = 0;
for(int i=0, n=height.length; i<n; i++){
while(st.size()>0 && height[st.peek()]<=height[i]){
int rm = i;
int curr = height[st.pop()];
if(st.size()==0) break;
int lm = st.peek();
while(st.size()>=2){
int i = st.pop();
int j = st.pop();
if(arr[i][j]==1){
st.push(j);
}else{
st.push(i);
}
}
4
0000 //celeb is knows no one arr[i][j]=0 i may be celeb.
1011 //everyone knows celeb arr[i][j]=1 j may be celeb;
1101
1110
ans: 0
-----------------------------------------------------------------