See More

package stackBasics; import java.util.ArrayDeque; //import java.util.Stack; public class MainClass { static int[] previousSmaller(int a[]) { int ans[] = new int[a.length]; ArrayDeque stack = new ArrayDeque<>(); int i = 0; for(int e: a) { while(!stack.isEmpty() && stack.peek() >= e) { stack.pop(); } if(stack.isEmpty()) { ans[i] = -1; } else { ans[i] = stack.peek(); } stack.push(e); i++; } return ans; } static boolean matchingParenthesis(String s) { ArrayDeque stack = new ArrayDeque<>(); for(int i = 0; i stack = new Stack<>(); // ArrayDeque stack = new ArrayDeque<>(); // // stack.push(2); // stack.push(4); // stack.push(1); // stack.push(8); // // System.out.println(stack.pop()); // System.out.println(stack.pop()); // System.out.println(stack.pop()); // System.out.println(stack.pop()); } }