Stack - Queue
Stack - Queue
public MyStack() {
arr = new int[1000];
top = -1;
}
MyQueue() {
front = 0;
rear = 0;
}
MyQueue() {
front = 0;
rear = 0;
}
if (nextRear != front) {
arr[rear] = x;
rear = nextRear;
}
// else: Queue is full, do nothing
}
MyQueue() {
front = 0;
rear = 0;
}
if (nextRear != front) {
arr[rear] = x;
rear = nextRear;
}
// else: Queue is full, do nothing
}
public MyStack() {
this.q = new LinkedList<>();
}
[Link](x);
for(int i=0;i<n;i++){
[Link]([Link]());
}
}
return [Link]();
}
public MyQueue() {
[Link] = new Stack<>();
[Link] = new Stack<>();
}
if ([Link]()) {
return -1; // Queue is empty
}
return [Link]();
}
if ([Link]()) {
return -1; // Queue is empty
}
return [Link]();
}
top = node;
}
return topValue;
}
}
class MyQueue
{
QueueNode front, rear;
if(rear == null){
//queue is empty
front = node;
rear = node;
}
else{
[Link] = node;
rear = node;
}
if(front == null){
//it means that was the only element in queue
rear = null;
}
return frontVal;
}
}
class Solution {
public boolean isValid(String s) {
int n = [Link]();
if(n%2 == 1){
//n is odd, that means parantheses is not balanced
return false;
}
for(int i=0;i<n;i++){
char c = [Link](i);
char popped=[Link]();
if((c==')'&&popped=='(')||(c=='}'&&popped=='{')||(c==']'&&popped=='['))
{
//ok, fine we got a valid bracket pair
continue;
}
else{
//bracket pair is not valid
return false;
}
}
}
Node top;
// Constructor
public MinStack() {
[Link] = null;
}
if (c == '(') {
[Link]('('); // Push opening bracket
}
else if (c == ')') {
// Pop until matching '(' is found
while ([Link]() != '(') {
[Link]([Link]());
}
[Link](); // Remove the '(' from the stack
}
else if (isOperator(c)) {
// Pop higher or equal priority operators before pushing current
while (![Link]() && [Link]() != '(' && priority(c) <= priority([Link]())) {
[Link]([Link]());
}
[Link](c); // Push current operator
}
else {
[Link](c); // Append operand (variable/constant) directly to output
}
}
if(c == '('){
[Link]('(');
}
else if(c == ')'){
while(![Link]() && [Link]() != '('){
[Link]([Link]());
}
if(![Link]()) {
[Link](); // pop out the open bracket
}
}
else if(isOperator(c)){
// For prefix: use strict less than (<) instead of less than or equal (<=)
// This ensures right associativity for operators like ^ in prefix notation
while(![Link]() && [Link]() != '(' && priority(c) < priority([Link]())){
[Link]([Link]());
}
[Link](c);
}
else{
// character (operand)
[Link](c);
}
}
return [Link]().toString();
}
if (c == '(') {
[Link](')');
}
else if (c == ')') {
[Link]('(');
} else {
[Link](c);
}
}
return [Link]();
}
return -1;
}
}
class Solution {
static String postToInfix(String exp) {
int n = [Link]();
for(int i=0;i<n;i++){
char c = [Link](i);
if(isOperator(c)){
String b = [Link]();
String a = [Link]();
[Link]("("+a+c+b+")");
}
else{
[Link]([Link](c));
}
}
for(int i=n-1;i>=0;i--){
char c = pre_exp.charAt(i);
if(isOperator(c)){
String b = [Link]();
String a = [Link]();
[Link]("("+b+c+a+")");
}
else{
[Link]([Link](c));
}
}
for(int i=0;i<n;i++){
char c = post_exp.charAt(i);
if(isOperator(c)){
String b = [Link]();
String a = [Link]();
[Link](c+a+b);
}
else{
[Link]([Link](c));
}
}
if(isOperator(c)){
String b = [Link]();
String a = [Link]();
[Link](b+a+c);
}
else{
[Link]([Link](c));
}
}
return ans;
}
}
class Solution {
public int[] nextGreaterElements(int[] nums) {
int n = [Link];
int[] ans = new int[n];
return ans;
}
}
class Solution {
public void nextSmaller(int arr[]) {
Stack<Integer> st = new Stack<>();
// For the current index, count elements to its right that are greater
for (int j = indices[i] + 1; j < N; j++) {
if (arr[j] > arr[indices[i]]) {
count++;
}
}
return ans;
}
}
class Solution {
public int trap(int[] height) {
int n = [Link];
prefixMaximum[0] = height[0];
for(int i=1; i<n; i++){
prefixMaximum[i] = [Link](height[i], prefixMaximum[i-1]);
}
suffixMaximum[n-1] = height[n-1];
for(int i=n-2; i>=0; i--){
suffixMaximum[i] = [Link](height[i], suffixMaximum[i+1]);
}
int waterUnits = 0;
for(int i=0; i<n; i++){
int leftMax = prefixMaximum[i];
int rightMax = suffixMaximum[i];
return waterUnits;
}
}
class Solution {
public int trap(int[] height) {
int n = [Link];
return waterUnits;
}
}
class Solution {
public int sumSubarrayMins(int[] arr) {
int mod = (int)1e9 + 7;
int n = [Link];
int ans = 0;
for(int i=0;i<n;i++){
int minInSubarr = Integer.MAX_VALUE;
for(int j=i;j<n;j++){
minInSubarr = [Link](minInSubarr, arr[j]);
ans = (ans + minInSubarr) % mod;
}
}
return ans;
}
}
class Solution {
public int sumSubarrayMins(int[] arr) {
int mod = (int)1e9 + 7;
int n = [Link];
long ans = 0;
return (int)ans;
}
[Link](i);
}
return nextSmallerElementIndex;
}
[Link](i);
}
return prevSmallerOrEqualElementIndex;
}
}
class Solution {
public int[] asteroidCollision(int[] asteroids) {
int n = [Link];
Stack<Integer> st = new Stack<>();
//destroy both
}
else if([Link]() || [Link]()<0){
//if the stack is empty (or) the asteroid is a negative element
[Link](asteroids[i]);
}
//if the stack asteroid is positive do nothing, it means the
current asteroid will be destroyed
}
}
return ans;
}
}
public long subArrayRanges(int[] nums) {
return sumSubarrayMax(nums) - sumSubarrayMin(nums);
}
// codes for Sum of Subarray Minimums
private long sumSubarrayMin(int[] arr){
int n = [Link];
long ans = 0;
return ans;
}
[Link](i);
}
return nextSmallerElementIndex;
}
[Link](i);
}
return prevSmallerOrEqualElementIndex;
}
long ans = 0;
return ans;
}
[Link](i);
}
return nextGreaterElementIndex;
}
[Link](i);
}
return prevGreaterOrEqualElementIndex;
}
class Solution {
public String removeKdigits(String num, int k) {
int n = [Link]();
if(k == n){
return "0"; //fast exit, we directly know 0 is the ans
}
// Pop digits while the current digit is smaller and we still have k to remove
while (k > 0 && ![Link]() && [Link]() > c) {
[Link]();
k--;
}
[Link](c);
}
[Link]();
int largestRectArea = 0;
// Find the index of the next smaller element (to the right)
int nextSmallerElementIndex = i;
while (nextSmallerElementIndex < n && heights[i] <= heights[nextSmallerElementIndex]) {
nextSmallerElementIndex++;
}
return largestRectArea;
}
}
class Solution {
public int largestRectangleArea(int[] heights) {
int n = [Link];
//pre-computing prevSmallerElementIndex
int[] prevSmallerElementIndex = new int[n];
for(int i=0; i<n; i++){
while(![Link]() && heights[[Link]()]>=heights[i]){
[Link]();
}
//pre-computing nextSmallerElementIndex
int[] nextSmallerElementIndex = new int[n];
for(int i=n-1; i>=0; i--){
while(![Link]() && heights[[Link]()]>=heights[i]){
[Link]();
}
return largestRectArea;
}
}
class Solution {
public int largestRectangleArea(int[] heights) {
int n = [Link];
Stack<Integer> st = new Stack<>(); // stack to store indices
int maxArea = 0;
// We process all bars and one extra imaginary bar of height 0 at the
end
for (int i = 0; i <= n; i++) {
// Use 0 as the height for the imaginary bar beyond the end
int currHeight = (i == n) ? 0 : heights[i];
[Link](i);
}
return maxArea;
}
}
class Solution {
public int maximalRectangle(char[][] matrix) {
int rows = [Link];
int cols = matrix[0].length;
int ans = 0;
for(int i=0; i<rows; i++){
ans = [Link](ans, largestRectangleArea(twoDimPrefixSum[i]));
}
return ans;
}
// We process all bars and one extra imaginary bar of height 0, just to process those bars
// that did not have any nextSmallerElement
int nextSmallerElementIndex = i;
int prevSmallerElementIndex = [Link]() ? -1 : [Link]();
[Link](i);
}
return maxArea;
}
}
class StockSpanner {
ArrayList<Integer> prices;
public StockSpanner() {
[Link] = new ArrayList<>();
}
[Link](price);
return count;
}
}
class StockSpanner {
public StockSpanner() {
[Link] = new Stack<>();
curIdx = -1; //-1 denotes the stock market has just opened no days
//curIdx will be the index of the today
}
return ans;
}
}
class Solution {
public int[] maxSlidingWindow(int[] nums, int k) {
//total number of subarrays will be n - k + 1
int n = [Link];
int[] ans = new int[n-k+1];
ans[i] = max;
}
return ans;
}
}
class Solution {
public int[] maxSlidingWindow(int[] nums, int k) {
int n = [Link];
int[] ans = new int[n-k+1];
for(int i=0;i<n;i++){
//the elements before i-k (including) can't be the part of window
while(![Link]() && [Link]() <= i-k){
[Link]();
}
//Remove all indices from the back whose corresponding values are smaller than nums[i]
// Because they cannot be the max if nums[i] is greater
//but if the cur value is smaller than the deque last, then it may become the max
//(after the element which is now max, is moved out of window)
while(![Link]() && nums[[Link]()] <= nums[i]){
[Link]();
}
return ans;
}
}
class Solution {
public int celebrity(int mat[][]) {
int n = [Link];
for(int i=0;i<n;i++){
for(int j=0;j<n;j++){
if(mat[i][j] == 1){
numPplIKnow[i]++;
numPplKnowsMe[j]++;
}
}
}
//either both knows each other, then they both can't be celeb
//or if no one knows each other then also no one can be celeb,
// because there will be 1 person left that didn't know the other
top++;
bottom--;
}
else if(mat[top][bottom] == 1){
//does the top knows bottom
//if yes top can't be celebrity
top++;
}
else if(mat[bottom][top] == 1){
//does the bottom knows top
//if yes bottom can't be celebrity
bottom--;
}
}
//loop ended
if(top > bottom){
//we don't have any celebrity candidate
return -1;
}
return top;
}
}
class LRUCache {
Map<Integer,Integer> cache;
int capacity;
if([Link]() == capacity){
//we do not have space in cache
// If this was the only node in oldList list, and that freq was the minimum
//update minFreq
if([Link] == 0 && oldFreq == minFreq) {
minFreq++;
}
return [Link];
}
// If this was the only node in oldList list, and that freq was the minimum
//update minFreq
if([Link] == 0 && oldFreq == minFreq) {
minFreq++;
}
// Remove the least recently used node from LFU list (from tail)
Node nodeToRemove = [Link](); //
minFreq = 1; //obv if a new block is created it is accessed for the least time (i.e 1)
}
}
}
//Implementation for DLL
class Node {
int key;
int value;
int freq;
Node prev;
Node next;
[Link] = null;
[Link] = null;
}
}
class DoublyLinkedList {
Node head;
Node tail;
int size;
public DoublyLinkedList() {
head = new Node(0, 0); // dummy head
tail = new Node(0, 0); // dummy tail
[Link] = tail;
[Link] = head;
size = 0;
}
//addition in O(1)
void addNode(Node node) {
[Link] = [Link];
[Link] = head;
[Link] = node;
[Link] = node;
size++;
}
//deletion in O(1)
void removeNode(Node node) {
[Link] = [Link];
[Link] = [Link];
//node doesn't havy anything pointing to it, so GC will trash it out
size--;
}
//O(1)
Node removeLast() {
if (size == 0){
return null;
}
Node last = [Link];
removeNode(last); //size will be decresed in the removeNode Call
return last;
}
}