Lecture 58: 'N' Stacks in an Array || Stack Hard Question

preview_player
Показать описание
In this Video, we are going to solve some of famous Stack Hard Question.

There is a lot to learn, Keep in mind “ Mnn bhot karega k chor yrr apne se nahi hoga ya maza nahi aara, Just ask 1 question “ Why I started ? “

Questions Links:

Do provide you feedback in the comments, we are going to make it best collectively.

Connect with me here:

Telegram Group Link: Love Babbar CODE HELP

Intro Sequence: We have bought all the required Licenses of the Audio, Video & Animation used.

Timestamps:

00:00 - Question 1
03:16 - Promotion
04:08 - Approach #1
08:50 - Approach #2
27:00 - Code

#DSABusted #LoveBabbar
Рекомендации по теме
Комментарии
Автор

I noticed one thing after sometime, *description* of this video. Jab bhi lage ki nahi hoga mujhse, remember "Why I started". Kam shabdon mein itni badi aur prernadaayak baat keh di bhaiya, bahut mast laga aur motivation bhi mila.

divyagupta
Автор

Zeherrr hai ye( next level ). Intuition kaise bnaye aise question ke optimised approach ki bhaiya. Aur agar ye question interviewer ne pucha aur agar answer kr diya toh interviewer khega ratt ke aaya hai 😂

kartikaymahajan
Автор

I left this question and jumped to next lecture. I kept in mind that after completing lecture 140, I will come back and watch this, then solve. It seemed so easy today, after dry run, it makes so much sense. A very good question.

inter
Автор

Ek dum Makkhan explanation diya bhaiya!! ❤
Time laga samajhne me..lekin jab samajh aane laga..to mazza hi aa gya..Ab Easy lag rha hai ✌🙏

RohitKumar-oolo
Автор

26:47
kaafi 💆 approach

Ye baar baar upar niche point dekh dekh ke teeno arrays update karne me confusion ho rha tha. Ek jagah sab arrays me samjha dete toh aur thoda better ho jata.
Second.
Correct me if I'm wrong.
I think steps order of Step 2-3 can be interchanged. to make approach make more sense.
Like this.
1) Get the index.
2) Insert the element in that index. Now, since array/stack has changed...
3) - update the freespot to have [next(index)] freespot.
4) - update next because we have to point to earlier inserted element {top[m-1]}
5)- and update top to point to index where element has been inserted. i.e index

vortexdeo
Автор

It's motivating to see 1500 people are still following this religiously and I am proud to be one of them

harpratapsinhnakoom
Автор

Bhaiya apke jaisa koi explain nahi kar sakta, thoda time laga but itna ache se samajh aya

nishant
Автор

Logic :
top[] size = no. of stacks, and it contains index of top element of respective stack. e.g top element of 2nd stack will be kept in [2-1]th stack.
if ith stack is empty then top[i] = -1;
we initialise top[] with -1 as all stacks are empty during initialisation.

freespot : denotes index of arr which is free, so we can push element there, and we need to update freespot to next available free spot!
we initialise freespot to 0 and if we are pushing an element then we'll update freespot to next free spot and that'll be 1 that's why we initialise next[] with 1! //next[0] = 1

Next: stores next free spot, eg. for i = 0, next free spot is 1, next[0] = 1;
//it's primary goal is to update freespot variable with next free index, after that we can store previous element in it why ? coz when we pop then we will have to update top of stack with previous element, and we can easily access it this way!.

next : 2 functions
1. it'll contain next free spot
2. after assigning freespot, it contains prev element index (it is actually current top element, after we push 'x' it'll become prev!)
*/


#include <bits/stdc++.h>
class NStack
{
int *arr, *top, *next, freespot;

public:
// Initialize your data structure.
NStack(int N, int 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
for (int i = 0; i < S; 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)
{
//check for overflow
if (freespot == -1) return false;
//freespot space index
int i = freespot;
//update freespot to next freespot space
freespot = next[i];//freespot = next[freespot]

//update next[i] to the current top element
next[i] = top[m-1];
//update top
top[m-1] = i;

//push element
arr[i] = x;

return true;
}

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

int i = top[m-1];
top[m-1] = next[i]; //coz next[i] contains prev element as well if it's not empty
/*
we have two free spots 1.the variable free spot 2nd is i, coz it's popped. i need to become free spot.

if we make freespot = i directly then how
*/
next[i] = freespot;
freespot = i;

return arr[i];
}
};

theOmKumar
Автор

yo youtube give this man a shield (most consistent youtuber) ;)

prathyyyyy
Автор

The Difference between you and others is the Time and Energy You Take to explain this Question. Hats off to you :)

ekanshsharma
Автор

Present Bhaiya Ji.
Series zabardast chal rhi h❤️

priyanshugoyal
Автор

This could be the toughest lecture in the playlist, but its actually not . All Thanks to you bhaiya, the way you explained was really impressive Thank u so much bhaiya moving forward to next lecture

ekanshsharma
Автор

Subah ka intezaar khatam....dhanyavaad bhaiya

mukulmishra
Автор

Bhaiya make video on project for Product based company(Faang).🙂
And salary negotiation in job interview..👍
Btw Consistancy ++👍👌

vinayaksharma-ysip
Автор

One of the best questions i have ever i was damn confused ki bhaiya kya kya likh rahe h smjha rahe jb dry run 45-50 minutes lge but crystal clear how easy this question was😊
Maza aagya guruju😂

SurajKumar-byfh
Автор

Love Bhaia is a Brand for Placement Aspirants....

vishalkumarshaw
Автор

Btw, even you complicated it quite a bit in the whole video somehow. But I understood almost everything when I saw that GFG video because their graphical and pictorial representation of all the arrays was in one page, so it was easier to follow. It wasn't as complicated as you mentioned it would be.

suvigyabasnotra
Автор

freespot se pehle sab samajh aa gaya uske baad toh bhaiyya har kuch bolne lage

rzeeshankhan
Автор

best explanation on YouTube for this question

sambhavsharma
Автор

Present bhaiya with lot's of fire and love

anuptewary