Stacks

Download as txt, pdf, or txt
Download as txt, pdf, or txt
You are on page 1of 13

___________________________________________________________________________Q.

38) 2
stacks in an array

https://www.codingninjas.com/codestudio/problems/two-stacks_983634?
leftPanelTab=0&campaign=YouTube_CodestudioLovebabbar5thfeb&utm_source=youtube&utm_m
edium=affiliate&utm_campaign=YouTube_CodestudioLovebabbar5thfeb

#include <bits/stdc++.h>
class TwoStack {

public:
int* arr;
int top1;
int top2;
int size;

// Initialize TwoStack.
TwoStack(int s) {
// Write your code here.
this->size = s;
top1 = -1;
top2 = s;
arr = new int[s];
}

// Push in stack 1.
void push1(int num) {
// Write your code here.
if((top2 - top1)>1){
top1++;
arr[top1]=num;

}
}

// Push in stack 2.
void push2(int num) {
// Write your code here.
if((top2 - top1)>1){
top2--;
arr[top2]=num;
}
}

// Pop from stack 1 and return popped element.


int pop1() {
// Write your code here.
if(top1>=0){
int ans = arr[top1];
top1--;
return ans;
}
else{
return-1;
}
}
// Pop from stack 2 and return popped element.
int pop2() {
// Write your code here.
if(top2<size){
int ans = arr[top2];
top2++;
return ans;
}
else{
return -1;
}
}
};

_________________________________________________________________________Q. 39)
reverse a string using stack

in vs code

__________________________________________________________________________Q. 40)
Delete middle element from stack

https://www.codingninjas.com/codestudio/problems/delete-middle-element-from-
stack_985246?
leftPanelTab=0&campaign=Lovebabbarcodestudio&utm_source=youtube&utm_medium=affiliat
e&utm_campaign=Lovebabbarcodestudio

#include <bits/stdc++.h>

void solve(stack<int>&inputStack, int count, int size){


//base case
if(count == size/2){
inputStack.pop();
return;
}

int num = inputStack.top();


inputStack.pop();

solve(inputStack, count+1, size);

inputStack.push(num);

void deleteMiddle(stack<int>&inputStack, int N){

int count = 0;
solve(inputStack, count , N);
}

___________________________________________________________________________Q. 41
Valid Parentheses

https://www.codingninjas.com/codestudio/problems/valid-parenthesis_795104?
topList=love-babbar-dsa-sheet-
problems&leftPanelTab=0&campaign=Lovebabbarcodestudio&utm_source=youtube&utm_medium
=affiliate&utm_campaign=Lovebabbarcodestudio

bool isValidParenthesis(string expression)


{
stack<char> s;

for(int i=0; i<expression.size(); i++){


char ch = expression[i];

if(ch == '(' || ch == '{' || ch == '['){


s.push(ch);
}
else{
if (!s.empty()) {
char top = s.top();
if ((ch == ')' && top == '(') || (ch == '}' && top == '{') || (ch ==
']' && top == '[')){
s.pop();
}
else{
return false;
}
}
else{
return false;
}
}
}
if(s.empty()){
return true;
}
else{
return false;
}
}
___________________________________________________________________________Q. 42)
Insert An Element At Its Bottom In A Given Stack

https://www.codingninjas.com/codestudio/problems/insert-an-element-at-its-bottom-
in-a-given-stack_1171166?topList=love-babbar-dsa-sheet-
problems&leftPanelTab=0%3Fsource
%3Dyoutube&campaign=Lovebabbarcodestudio&utm_source=youtube&utm_medium=affiliate&ut
m_campaign=Lovebabbarcodestudio

#include <bits/stdc++.h>

void solve(stack<int>& myStack, int x, int count,int n){


//base case
if(count == n){
myStack.push(x);
return;
}

int num = myStack.top();


myStack.pop();

solve(myStack,x,count+1,n);

myStack.push(num);
}

stack<int> pushAtBottom(stack<int>& myStack, int x)


{
int n = myStack.size();

int count = 0;

solve(myStack,x,count,n);

return myStack;

______________________________________________________________________________Q.
43) Reverse Stack Using Recursion

https://www.codingninjas.com/codestudio/problems/reverse-stack-using-
recursion_631875?topList=love-babbar-dsa-sheet-problems&leftPanelTab=0%3Fsource
%3Dyoutube&campaign=Lovebabbarcodestudio&utm_source=youtube&utm_medium=affiliate&ut
m_campaign=Lovebabbarcodestudio

void insertAtBottom(stack<int> &stack, int top){


if(stack.empty()){
stack.push(top);
return;
}

int num = stack.top();


stack.pop();

insertAtBottom(stack,top);

stack.push(num);
}

void reverseStack(stack<int> &stack) {


//base case
if(stack.empty()){
return;
}

int top = stack.top();


stack.pop();

//recursive call
reverseStack(stack);

insertAtBottom(stack,top);
}

___________________________________________________________________Q. 44 Sort a
Stack

https://www.codingninjas.com/codestudio/problems/sort-a-stack_985275?topList=love-
babbar-dsa-sheet-
problems&leftPanelTab=0&campaign=Lovebabbarcodestudio&utm_source=youtube&utm_medium
=affiliate&utm_campaign=Lovebabbarcodestudio

#include <bits/stdc++.h>

void sortInsert(stack<int> &stack , int element){


//base case

if(stack.empty() || (!stack.empty() && element>=stack.top())){


stack.push(element);
return;
}

int t = stack.top();
stack.pop();

sortInsert(stack,element);

stack.push(t);
}

void sortStack(stack<int> &stack)


{
//base case
if(stack.empty()){
return;
}

int top = stack.top();


stack.pop();

//recursion call
sortStack(stack);

sortInsert(stack,top);

_________________________________________________________________________________Q.
45) Redundant bracket

https://www.codingninjas.com/codestudio/problems/redundant-brackets_975473?
leftPanelTab=0%3Fsource
%3Dyoutube&campaign=Lovebabbarcodestudio&utm_source=youtube&utm_medium=affiliate&ut
m_campaign=Lovebabbarcodestudio

#include <bits/stdc++.h>
bool findRedundantBrackets(string &s)
{
stack<char> st;
for(int i=0; i<s.size(); i++){
char ch = s[i];
if(ch == '(' || ch == '+' || ch == '-' || ch == '*' || ch == '/'){
st.push(ch);
}
else{
if(ch == ')'){
bool isRedundant = true;
while(st.top()!='('){

if(st.top()=='-' || st.top()=='+' || st.top()=='*' ||


st.top()=='/'){
isRedundant = false;
}
st.pop();
}
st.pop();
if(isRedundant == true){
return true;
}
}
}
}
return false;
}

___________________________________________________________________________________
___Q.46 Minimum Cost To Make String Valid

https://www.codingninjas.com/codestudio/problems/minimum-cost-to-make-string-
valid_1115770?
leftPanelTab=0&campaign=Lovebabbarcodestudio&utm_source=youtube&utm_medium=affiliat
e&utm_campaign=Lovebabbarcodestudio

#include <bits/stdc++.h>
int findMinimumCost(string str) {
if(str.size()%2 == 1){
return -1;
}
stack<char> s;
for(int i=0; i<str.size(); i++){
char ch = str[i];
if(ch == '{'){
s.push(ch);
}
else{
if(!s.empty() && s.top() == '{'){
s.pop();
}
else{
s.push(ch);
}
}
}
//invalid string left
int a=0,b=0;
while(!s.empty()){
if(s.top()=='{'){
a++;
}
else{
b++;
}
s.pop();
}
int ans = (a+1)/2 + (b+1)/2;
return ans;
}
_______________________________________________________________________________Q.
47) Next Smaller Element

https://www.codingninjas.com/codestudio/problems/next-smaller-element_1112581?
topList=love-babbar-dsa-sheet-
problems&leftPanelTab=0&campaign=Lovebabbarcodestudio&utm_source=youtube&utm_medium
=affiliate&utm_campaign=Lovebabbarcodestudio

#include<bits/stdc++.h>
vector<int> nextSmallerElement(vector<int> &arr, int n)
{
stack<int> s;
s.push(-1);
vector<int> ans(n);

for(int i = n-1; i>=0; i--){


int curr = arr[i];
while(s.top()>=curr){
s.pop();
}
ans[i] = s.top();
s.push(curr);
}
return ans;
}

__________________________________________________________________________________Q
.48) Largest Rectangle in Histogram

https://leetcode.com/problems/largest-rectangle-in-histogram/description/

vector<int> nextSmallElement(vector<int>& heights, int n){


stack<int> s;
s.push(-1);
vector<int> ans(n);
for(int i=n-1; i>=0; i--){
int curr = heights[i];
while(s.top()!=-1 && curr<=heights[s.top()]){
s.pop();
}
ans[i] = s.top();
s.push(i);
}
return ans;
}

vector<int> prevSmallElement(vector<int>& heights, int n){


stack<int> s;
s.push(-1);
vector<int> ans(n);
for(int i=0; i<n; i++){
int curr = heights[i];
while(s.top()!=-1 && curr<=heights[s.top()]){
s.pop();
}
ans[i] = s.top();
s.push(i);
}
return ans;
}

int largestRectangleArea(vector<int>& heights) {


int n = heights.size();

vector<int> next(n);
next = nextSmallElement(heights,n);

vector<int> prev(n);
prev = prevSmallElement(heights,n);

int area = INT_MIN;


for(int i=0; i<n; i++){
int l = heights[i];
if(next[i]==-1){
next[i] = n;
}
int b = next[i]-prev[i] -1;
int newArea = l*b;
area = max(area,newArea);
}
return area;
}

________________________________________________________________________________Q.4
9) The Celebrity Problem

https://practice.geeksforgeeks.org/problems/the-celebrity-problem/1

int celebrity(vector<vector<int> >& M, int n)


{
stack<int> s;
for(int i=0; i<n; i++){
s.push(i);

}
while(s.size()>1){
int a = s.top();
s.pop();
int b = s.top();
s.pop();

if(M[a][b] == 1){
s.push(b);
}
else{
s.push(a);
}
}

int ans = s.top();


int count0 = 0;
int count1 = 0;
for(int i=0; i<n; i++){
if(M[ans][i] == 0){
count0++;
}
}
for(int i=0; i<n; i++){
if(M[i][ans] == 1){
count1++;
}
}
if(count0 == n && count1 == n-1){
return ans;
}
else{
return -1;
}
}

_______________________________________________________________________________Q.50
) Max rectangle

https://practice.geeksforgeeks.org/problems/max-rectangle/1

vector<int> nextSmallElement(int* heights, int n){


stack<int> s;
s.push(-1);
vector<int> ans(n);
for(int i=n-1; i>=0; i--){
int curr = heights[i];
while(s.top()!=-1 && curr<=heights[s.top()]){
s.pop();
}
ans[i] = s.top();
s.push(i);
}
return ans;
}

vector<int> prevSmallElement(int* heights, int n){


stack<int> s;
s.push(-1);
vector<int> ans(n);
for(int i=0; i<n; i++){
int curr = heights[i];
while(s.top()!=-1 && curr<=heights[s.top()]){
s.pop();
}
ans[i] = s.top();
s.push(i);
}
return ans;
}

int largestRectangleArea(int* heights,int n) {

vector<int> next(n);
next = nextSmallElement(heights,n);

vector<int> prev(n);
prev = prevSmallElement(heights,n);

int area = INT_MIN;


for(int i=0; i<n; i++){
int l = heights[i];
if(next[i]==-1){
next[i] = n;
}
int b = next[i]-prev[i] -1;
int newArea = l*b;
area = max(area,newArea);
}
return area;
}

int maxArea(int M[MAX][MAX], int n, int m) {


int area = largestRectangleArea(M[0],m);

for(int i = 1; i<n; i++){


for(int j=0; j<m; j++){
//row update by adding previous row value
if(M[i][j] != 0){
M[i][j] = M[i][j] + M[i-1][j];
}
else{
M[i][j] = 0;
}
}
//entire row is updated
area = max(area, largestRectangleArea(M[i],m));
}
return area;
}

________________________________________________________________________________Q.5
1) N Stacks In An Array

https://www.codingninjas.com/codestudio/problems/n-stacks-in-an-array_1164271?
topList=love-babbar-dsa-sheet-
problems&leftPanelTab=0&campaign=Lovebabbarcodestudio11thfeb&utm_source=youtube&utm
_medium=affiliate&utm_campaign=Lovebabbarcodestudio11thfeb

#include <bits/stdc++.h>
class NStack
{
int *arr;
int *top;
int *next;
int freespot;
int n,s;
public:
// Initialize your data structure.
NStack(int N, int S)
{
n = N;
s = S;
arr = new int[s];
top = new int[n];
next = new int[s];

//top initialise
for(int i=0; i<n; i++){
top[i] = -1;
}

//next initialise
for(int i=0; i<s-1; i++){
next[i] = i+1;
}
next[s-1] = -1;

freespot = 0;
}

// Pushes 'X' into the Mth stack. Returns true if it gets pushed into the
stack, and false otherwise.
bool push(int x, int m)
{
if(freespot == -1){
return false;
}
//find index
int index = freespot;

//insert in array
arr[index] = x;

//update freespot

freespot = next[index];

//update next
next[index] = top[m-1];

//update top
top[m-1] = index;

return true;
}

// Pops top element from Mth Stack. Returns -1 if the stack is empty, otherwise
returns the popped element.
int pop(int m)
{
if(top[m-1] == -1){
return -1;
}

int index = top[m-1];

top[m-1] = next[index];

next[index] = freespot;

freespot = index;

return arr[index];
}
};

You might also like

pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy