Stack Implementation using a Single Queue

Sdílet
Vložit
  • čas přidán 5. 09. 2024

Komentáře • 108

  • @Aks-47
    @Aks-47 Před 3 lety +96

    "some just give road maps and make videos" and here you find the actual content you need to follow.. Great work!

    • @souvikpanda7797
      @souvikpanda7797 Před 3 lety +3

      Bhai tum aaj har jagah mujhe comment sec me dikh rhe ho yaar :-)

    • @Aks-47
      @Aks-47 Před 3 lety +2

      @@souvikpanda7797 aur kahan dekhe ho😂

    • @ani68
      @ani68 Před 2 lety +1

      True 💯

    • @thepriestofvaranasi
      @thepriestofvaranasi Před 2 lety +3

      Love Babbar - Ye lo ek roadmap aur isse sikh lo😂

    • @rickseth9207
      @rickseth9207 Před 6 měsíci

      @@thepriestofvaranasi aise bewakoof baate karte ho tum log jisme logic nahi hota aur tum log dsa mai logic banake jindegi mai aage badhne ki sochte ho?😂Love Babbar ne roadmap bhi diya hai + pura free c++ ka placement based dsa playlist bhi banaya hai, which is genuinely good if one religiously follows his whole playlist, but y'all will hate no matter what and compare. Both Striver and Love Babbar are good in their own way and they both are genuinely awesome.

  • @ayushjain7130
    @ayushjain7130 Před 3 lety +41

    Wow, I never knew this approach by using a single queue, great explanation 👏

  • @anshitmishra
    @anshitmishra Před 3 lety +14

    We are so fortunate to have an innovator like you. Never seen this method before ❤

  • @RohitKumar-dz8dh
    @RohitKumar-dz8dh Před rokem +7

    You not only teach, you make feel how to do programming ❤ no need to go for code after getting approach.

    • @sunitasharma3016
      @sunitasharma3016 Před měsícem +2

      if you dont code you will forget

    • @RohitKumar-dz8dh
      @RohitKumar-dz8dh Před měsícem +1

      @@sunitasharma3016 i meant no need to find code from other, after getting approach we can write code our self.

  • @willturner3440
    @willturner3440 Před 3 lety +6

    Single queue solution was really what I want 🙌

  • @stith_pragya
    @stith_pragya Před 4 měsíci +2

    UNDERSTOOD.......Thank You So Much for this wonderful video.............🙏🏻🙏🏻🙏🏻🙏🏻🙏🏻🙏🏻

  • @rutikdeshmukh9373
    @rutikdeshmukh9373 Před rokem +1

    Wow, what a method of implementing stack using single queue, hats off!!!

  • @sriharsha5867
    @sriharsha5867 Před 6 měsíci

    The approach you taught for implementing stack using single array works only for LinkedList implementation but not with array implementation however swapping of elements works with array impl.

  • @helloclass2103
    @helloclass2103 Před 3 lety +3

    Bhaijaan kya samjha rahe ho❤️❤️,Matalb maine abhi c start ki hai aur mujhe bhi ye concept yaad hogya...are bhai❤️❤️

  • @koushik1261
    @koushik1261 Před 3 lety +9

    That single queue method is so simple, yet so effective 💥

  • @dylindian
    @dylindian Před 3 měsíci +2

    I am confused if this was implemented with a single queue or two queues. Please can someone explain?

  • @shreyanshmittal9381
    @shreyanshmittal9381 Před rokem +1

    very clever, amazed by the single queue implementation! keep up the great work striver bhai!

  • @user-zp8rd6ct6e
    @user-zp8rd6ct6e Před 3 měsíci +2

    Should not the TC be O(N^2) as the elements are being copied from q1 to q2 for every value of /n??

  • @AbhishekSharma-ip4df
    @AbhishekSharma-ip4df Před rokem +10

    The issue is with q.top() at 10:06 . The queue container in C++ does not have a top() member function. Instead, you should use q1.front() to access the front element of the queue.

  • @venkateshvenky2880
    @venkateshvenky2880 Před rokem +1

    Great approach using only single queue

  • @hardikpatel352
    @hardikpatel352 Před 2 měsíci +1

    Thanks

  • @U2011-n7w
    @U2011-n7w Před rokem +1

    best explanation for this question

  • @aditya_sipani
    @aditya_sipani Před 3 měsíci +1

    wont using 2 queues it will have space complexity as o(2n) because there are 2 queues used

    • @aditya_sipani
      @aditya_sipani Před 3 měsíci

      if not then dont you think,we havent optimised it ,if they both are giving same space and time complexity

  • @abdulhadi-me8gj
    @abdulhadi-me8gj Před 2 lety +9

    how come time complexity is O(n)?
    When we are pushing n-1 elements for each element. It would be somewhere O(nx(n-1))
    correct me if I am wrong

    • @namansharma7328
      @namansharma7328 Před 2 lety +6

      I think, he is talking about a single push operation. In a single push operation, we are just using a for loop which is running for (size-1) times. So at the worst case the TC would be O(n). (assuming the push operation in queue inside the for loop takes constant time. )
      Correct me if I am wrong.

  • @codespector1989
    @codespector1989 Před 2 lety

    finally I understood after seeing lots of explanation , thanks

  • @paramsratanpal6551
    @paramsratanpal6551 Před rokem

    i have done slightly different,correct me if i am wrong,i am adding the elements in normal fashion,but while pooping i m first removing it ,and then adding it at top

  • @gaganbedi4332
    @gaganbedi4332 Před 2 lety +1

    Channel Subscribed! Short, to the point and understable. 🤓

  • @engineer2104
    @engineer2104 Před 4 měsíci +1

    Thank you striver bhai ☺❣

  • @suryakumarsahani4057
    @suryakumarsahani4057 Před 3 lety +1

    Well explained, thankyou!!

  • @soumyajitpatnaik4399
    @soumyajitpatnaik4399 Před 2 lety

    should by Q1.push(x) in 6:23.

  • @SaumyaSharma007
    @SaumyaSharma007 Před 2 lety +2

    Long live Bhaiya.....
    Thank you for your efforts ☺️

  • @chitranshkumar6694
    @chitranshkumar6694 Před 3 lety +2

    Thanks bhaiya you are doing great job. Keep making videos like this

  • @wasifirshad9818
    @wasifirshad9818 Před 3 lety

    BEST VDO!
    Got 2 ways to approach the question

  • @OIAOa
    @OIAOa Před 7 měsíci +1

    Thanks bhaiya ❤

  • @Munchen888
    @Munchen888 Před 6 měsíci

    Also to implement Stack we can use a Linked List. But I suppose it will not be efficient solution.

  • @sharonthequeen28
    @sharonthequeen28 Před 4 měsíci +1

    what do you mean when you swap? is it liggite operation?

    • @rishabhgupta1222
      @rishabhgupta1222 Před 3 měsíci +1

      it's not exactly a swap, probably u could say shifting of the elements from Q2 to Q1

  • @vaibhavsharma8465
    @vaibhavsharma8465 Před 3 lety +4

    Literally... Mind blowing man 🔥🔥🔥🔥

  • @VivekSharma-eh2tv
    @VivekSharma-eh2tv Před 2 měsíci +1

    understood

  • @sapnakumari5456
    @sapnakumari5456 Před 2 lety

    i have never seen this approach before

  • @aniruddhachunne3011
    @aniruddhachunne3011 Před 2 lety +1

    Awesome... good explanation

  • @hemantranjan2297
    @hemantranjan2297 Před 2 lety +1

    small typo, q.front() instead of q.top()

  • @isms-7
    @isms-7 Před 2 lety

    thank u for such an awesome explanation striver

  • @saisurya.kattamuri
    @saisurya.kattamuri Před rokem

    Implementing Stack with Queue or Queue with Stack, is there a real life situation when we need to do this or is it only for sharpening brain as a problem to solve?

  • @aadityabedi3140
    @aadityabedi3140 Před 3 lety +3

    brother wont the space complexity in second approach be O(1) as we are using the queue itself

    • @takeUforward
      @takeUforward  Před 3 lety +6

      Wo queue ka space nai hoga kya 😵‍💫

    • @aadityabedi3140
      @aadityabedi3140 Před 3 lety

      @@takeUforward brother fer space aur time complexity dono appraoches ki same hi aa rhi aise toh, interviewer ko koi bhi bta skte approach?

    • @ayushjain7130
      @ayushjain7130 Před 3 lety +5

      @@aadityabedi3140 Yes in asymptotic analysis both will be same, but interviewer may ask to use only single queue...
      Like sometimes, if you are doing something on O(N) time but using 2 passes(two independent loops), they may ask to do it in single pass (if possible)

  • @anshumanpanigrahi7817
    @anshumanpanigrahi7817 Před 3 lety +2

    Genius 💫

  • @sarangtamrakar8723
    @sarangtamrakar8723 Před 2 lety

    Awesome Approach

  • @pranavgawade1546
    @pranavgawade1546 Před 3 lety +3

    Watch in 1.25x for a better experience XD

  • @Gamerz-g4i
    @Gamerz-g4i Před měsícem

    thnx

  • @rashmimittal5405
    @rashmimittal5405 Před 3 lety

    What if I wanna insert negative elements..
    Will pop not create a problem?

  • @dharmaraosalana7977
    @dharmaraosalana7977 Před 3 lety +1

    Bro What is the difference between cp course in gfg vs dsa course in gfg. Can you help what to prefer?

  • @abhishekgarg6683
    @abhishekgarg6683 Před 2 lety +1

    Absolutely loved the explanation

  • @sheetalwanave9750
    @sheetalwanave9750 Před 3 lety +1

    using two queue our result is 3,4,2 and using one queue our result is 3,2,4 why so? please reply

    • @039_cse_jayamaharajaa.s.v5
      @039_cse_jayamaharajaa.s.v5 Před 2 lety

      Check the question properly.. first he used 3,4,2 for pushing in two queue but in single queue method , he pushed in the order of 3,2,4

  • @codeman3828
    @codeman3828 Před 6 měsíci

    Understood

  • @pranav288
    @pranav288 Před 2 lety

    10:55 Why should we do the unoptimized approach before ? Makes as an extra question to remember

  • @shivangyadav5887
    @shivangyadav5887 Před 2 lety

    Sir I have query that we are just trying reverse the order of insertion so we are reversing the elements
    For that i am assuming that our queue is a stack if I add x to it it will added to end of queue but since it must obey stack property it must me added front of queue so we empty our queue elements and add x to it,then add all elements that we removed in order of the removal

    • @dsadaddy8403
      @dsadaddy8403 Před 2 lety

      WHERE YOU WILL STORE THE REMOVED ELEMENTS? IT IS BUT OBVIOUS THAT YOU WILL NEED EXTRA O(N) SPACE FOR THAT.
      -

  • @mayanksharma6254
    @mayanksharma6254 Před 3 lety

    where can i find the all question at one place? is there any google sheet for that too?

  • @user-tk2vg5jt3l
    @user-tk2vg5jt3l Před 6 měsíci

    Thank you Bhaiya

  • @roughuse.jaiganesh
    @roughuse.jaiganesh Před 2 lety

    understood.

  • @sarangkumarsingh7901
    @sarangkumarsingh7901 Před 2 měsíci

    Awesome.

  • @ankitchoudhary1862
    @ankitchoudhary1862 Před 3 lety +1

    Came here just to like the video to complete 🎯

  • @shivbhaktamy
    @shivbhaktamy Před rokem

    Thanks Striver Bro

  • @dsadaddy8403
    @dsadaddy8403 Před 2 lety

    BROTHER HOW CAN WE HANDLE DUPLICATE ELEMENTS HERE

  • @NinjaCoders
    @NinjaCoders Před rokem

    Thank you bhaiya fo making this content

  • @satyampande684
    @satyampande684 Před 2 lety

    understood!!

  • @exoticme4760
    @exoticme4760 Před rokem

    thnk u

  • @dreamyme543
    @dreamyme543 Před 2 lety

    Understood 👍

  • @lakshmiprasanna7058
    @lakshmiprasanna7058 Před rokem

    Understood 💯💯💯

  • @Harsh-jc2bz
    @Harsh-jc2bz Před 4 měsíci +1

    Nice 30-04-2024

  • @exoticme4760
    @exoticme4760 Před rokem

    complexity for both is same so how using a single queue is optimized?

  • @yashodeepdhas8408
    @yashodeepdhas8408 Před 2 lety

    Awesome !!!

  • @jeevanreddy9895
    @jeevanreddy9895 Před rokem

    Why don't universities hire people like them??

  • @prepmee
    @prepmee Před 2 lety

    wonderful video. Maza aa gaya 1.25x me

  • @aasifali9139
    @aasifali9139 Před rokem

    thx sir

  • @mohithguptakorangi1766

    Why many links in the SDE sheet are missing? after Day 13....???

    • @takeUforward
      @takeUforward  Před 3 lety

      Because videos are not uploaded.

    • @mohithguptakorangi1766
      @mohithguptakorangi1766 Před 3 lety

      @@takeUforward so this series will take another year to complete?😶

    • @takeUforward
      @takeUforward  Před 3 lety

      @@mohithguptakorangi1766 you have the problems, try it yourself.. i have uploaded 100+ problems :)

    • @mohithguptakorangi1766
      @mohithguptakorangi1766 Před 3 lety

      @@takeUforward Ok thank you, I'll try my best

  • @ashutoshjha5356
    @ashutoshjha5356 Před 3 lety

    awesome awesome awesome awesome

  • @shivamyadav6369
    @shivamyadav6369 Před 3 lety

    Just osm bhai🔥

  • @krishnarajs929
    @krishnarajs929 Před 2 lety

    using same queue why not we simply return a[rear-1%n] for top() ?
    and for pop() insert dummy val in a[rear-1%n] then rear-- ; will this work?

    • @dsadaddy8403
      @dsadaddy8403 Před 2 lety

      BROTHER TOP IS IN THE FRONT ELEMENT OF THE QUEUE AND CANT BE ACCESSED IN THE REAR END.

  • @sunnykakrani7830
    @sunnykakrani7830 Před 3 lety

    striver bhaiya bada hi chikna padate he !!!

  • @subhamengine1143
    @subhamengine1143 Před 3 lety

    nice one

  • @shubhamkhare26
    @shubhamkhare26 Před 3 lety

    Nice video

  • @nitishprasadkushwaha
    @nitishprasadkushwaha Před 2 lety

    u are the bestttt

  • @utkarshgupta3869
    @utkarshgupta3869 Před 3 lety

    Hello bhaiya,
    I have one question... please reply...
    I am a B.Sc(c.s) Student.
    Please help me to know can I get a job(above 5 lpa).
    I learnt enough C++ and completed DSA and going to start web development and also 3 star at codechef.
    I can learn anything for job after Graduation.
    Please help to me tell that can I get or I have to do MCA.

    • @janvibuddhadev5367
      @janvibuddhadev5367 Před 3 lety +1

      Hi Utkarsh, i saw this same comment on linkedIn post also...... So, i thought let me help you out, Don't worry you are doing very good. you don't need MCA. u will find job which will give you more than 5 lpa easily. just keep going......
      I am pursuing masters (M.Sc IT) from DAIICT and i am a 2 star coder at codechef. So, Comparing the your situation with me you have done a greate job......

    • @utkarshgupta3869
      @utkarshgupta3869 Před 3 lety

      @@janvibuddhadev5367 thank you

  • @rishabhkumar8115
    @rishabhkumar8115 Před 3 lety

    woow

  • @aasthajha1051
    @aasthajha1051 Před 3 lety

    great great great!!!

  • @NitishKumar-xr9tx
    @NitishKumar-xr9tx Před 2 lety

    After watching half of the video able to solve @ leetcode

  • @argha00
    @argha00 Před 7 měsíci

    2 stack || java
    import java.util.LinkedList;
    class MyStack {
    Queue q1;
    Queue q2;
    public MyStack() {
    q1 = new LinkedList();
    q2 = new LinkedList();
    }

    public void push(int x) {
    q2.offer(x);
    while (!q1.isEmpty()) {
    q2.offer(q1.poll());
    }
    Queue temp = q1;
    q1 = q2;
    q2 = temp;
    }

    public int pop() {
    return q1.poll();
    }

    public int top() {
    return q1.peek();
    }

    public boolean empty() {
    return q1.isEmpty();
    }
    }

  • @ashokdurunde1814
    @ashokdurunde1814 Před 2 lety +1

    Single queue solution was really what I want 🙌

  • @chiragbansod8252
    @chiragbansod8252 Před 5 měsíci +1

    understood

  • @himanshu_vcan
    @himanshu_vcan Před rokem

    thanks

  • @roopeshn3301
    @roopeshn3301 Před 2 lety

    Understood