Sliding Window Maximum | Solution

Поделиться
HTML-код
  • Опубликовано: 19 окт 2024

Комментарии • 159

  • @atish4185
    @atish4185 3 года назад +50

    One of the most underrated channels in coding. You're fabulous Sir! :)

    • @Pepcoding
      @Pepcoding  3 года назад +7

      Thank you for appreciating.
      The love and respect which I get from you people keep me highly motivated and the same I am able to forward It to you people through my videos.
      So, keep motivating, keep learning and keep loving Pepcoding😊

    • @firozalam2749
      @firozalam2749 Год назад

      @@Pepcoding what is the meaning of pepcoding /

  • @adilkhatri7475
    @adilkhatri7475 2 года назад +16

    that J < I condition is very important to keep the time complexity O(n) otherwise in case of ascending order array the time complexity will be O(n*k).

    • @Pepcoding
      @Pepcoding  2 года назад

      For more detailed and curated content on DSA, Web Dev etc visit nados.pepcoding.com :)

  • @viditvaish7317
    @viditvaish7317 9 месяцев назад +1

    amazing ,aapse accha dsa youtube pr koi bhi nahi explain kr pata ,agar aapki videos na mile to ques fas jata hai ya samajhne me bahut time lagta hai baki jaigah se ,aapse ek baar me clear ho jata hai.Thanq

  • @anmolgirdhar4473
    @anmolgirdhar4473 3 года назад +8

    The best solution for interviews, I did meditation for the 3 points, and now I am feeling confident for the WHY of the solution.

    • @Pepcoding
      @Pepcoding  3 года назад +7

      Thankyou beta!
      I am glad you liked it. I hope that you are watching till the end and trying to understand what, how, and especially why of the problem.
      If you like our efforts, will you like to write a few words about us here (www.quora.com/What-are-the-good-websites-to-learn-data-structures-and-algorithms )

  • @ankoor
    @ankoor 3 года назад +27

    One of the best solutions. So intuitive!

    • @Pepcoding
      @Pepcoding  3 года назад +5

      Glad to know that you liked the content and thank you for appreciating.
      Keep motivating, keep learning and keep loving Pepcoding😊
      If you like our efforts, will you like to write a few words about us here (www.quora.com/How-do-I-start-learning-or-strengthen-my-knowledge-of-data-structures-and-algorithms )

  • @AyushKumar-fk5lm
    @AyushKumar-fk5lm 3 года назад +5

    Sumeet Sir Amazing!!!!!! Aapke alawa is question ko itne achhe se koi nahi samjha sakta! It took a lot lot effort. Pranaam aapko _/\_

    • @Pepcoding
      @Pepcoding  3 года назад +3

      Glad to know that you liked the content and thank you for appreciating.
      The love and respect which I get from you people keep me highly motivated and the same I am able to forward It to you people through my videos.
      So, keep motivating, keep learning and keep loving Pepcoding😊

  • @rohandsouza9147
    @rohandsouza9147 3 года назад +3

    The graph representation really helped me to understand the concept, I'll remember this for my life !! Definitely recommending this channel to my friends

  • @nomadshubham3907
    @nomadshubham3907 3 года назад +1

    And finally I follow only pepcoding for all coding related stuff...
    Isse badiya kuch nahi 🔥😀

  • @tanishmahajan2060
    @tanishmahajan2060 9 месяцев назад

    very intuitive best solution much better than deque one highly appreciated

  • @harshitasingh963
    @harshitasingh963 3 года назад

    Youe explanantion is undoubtedly the best !

  • @pulkitnijhawan653
    @pulkitnijhawan653 3 года назад +3

    There are a lot of solutions on the net using a deque but this is his is highly intuitive ...Thanks for posting these solutions...but doing j=i everytime instead of only when j

    • @Pepcoding
      @Pepcoding  3 года назад +4

      Thankyou beta!
      I am glad you liked it. I also hope that you are watching till the end and trying to understand the what, how, and especially why of the problem. If you like our efforts, will you like to review us here - g.page/Pepcoding/review?rc

    • @arpitpachauri9189
      @arpitpachauri9189 3 года назад

      yes i was also getting TLE when solving on leetcode.

  • @ritikakumar818
    @ritikakumar818 2 года назад +1

    one of the amazing explanation of this question.

    • @Pepcoding
      @Pepcoding  2 года назад

      Thanks Ritika :) If you want more content like this please visit nados.pepcoding.com

  • @PraveenKumar-ch5is
    @PraveenKumar-ch5is 3 года назад +19

    If the numbers are in increasing order, you are essentially check each element in the window . Then time complexity will be same as brute force

  • @anishsuman1371
    @anishsuman1371 3 года назад +5

    If array is sorted in ascending order time complexity will be 0(n*k) so worst case complexity will be n*k correct me if I am wrong

    • @gaunikasrivastava7851
      @gaunikasrivastava7851 3 года назад +2

      Yeah, you are right

    • @shushantgaur9420
      @shushantgaur9420 3 года назад +5

      @@gaunikasrivastava7851 @Anish suman
      actually it will still be O(n). i belive your concern is related to the upgradation of j = next_grater_of_j[ j ]; since we are not starting j from I, it will only take O(1) time every time. if we start j from I each time then of course time complexity will be O(n*k). for elements 1 2 3 4 5 6 7 8 9. if window size is 4. then first time while loop will run 3 times and next greater element will be 4. next time same while loop will start from 4 and will run only once i.e. till 5. and then only once till 6 and so on. so we get O(N) solution . however if we start j =i each time then time complexity increases to O(N*K).

    • @anishsuman1371
      @anishsuman1371 2 года назад

      @@shushantgaur9420 Yes man you are right

    • @shivamnegi7552
      @shivamnegi7552 Год назад

      @@anishsuman1371 kiss then

  • @tanyakemkar310
    @tanyakemkar310 3 года назад

    Best Solution with best explanation

  • @mishra1576
    @mishra1576 3 года назад +1

    Far better then those boring " PHD'S " Professors explanation....

  • @JaiSagar7
    @JaiSagar7 3 года назад +9

    sir this question consume my whole day 😭

  • @kavyansh8213
    @kavyansh8213 Год назад

    sir 1 small doubt here say i have an array 1 8 12 16 and then the next greater array would be 8 12 16 4 but when you wrote the statement nge[j]

    • @kavyansh8213
      @kavyansh8213 Год назад +1

      ok got it actually it is the index of elements that is being stored in next greater element array thanks sumit sir!

    • @nivealokhande2153
      @nivealokhande2153 Год назад +1

      @@kavyansh8213 i had same doubt, but u cleared it, thnxx

  • @411_danishranjan9
    @411_danishranjan9 3 года назад +1

    whats the time complexity of this approach?
    on leetcode , this approach is showing TLE

  • @vrajeshbadgujar
    @vrajeshbadgujar Год назад

    God level explaination!

  • @tusaarkhana3773
    @tusaarkhana3773 3 года назад

    Thank u sp much sir for ur efforts and motivational reels.

  • @devanshmesson2777
    @devanshmesson2777 2 года назад +5

    The time complexity would still be O(N*K).
    Take the case when array is sorted in increasing order.

  • @ojasvityagi9569
    @ojasvityagi9569 4 года назад +3

    Thankyou sir! Amazing video.
    sir, please post one for max frequency stack too. Thankyou!

  • @rishabhgoyal8110
    @rishabhgoyal8110 2 года назад

    So intuitive! Hats-off & God-bless you!

    • @Pepcoding
      @Pepcoding  2 года назад

      Many many thanks. For better experience visit on nados.pepcoding.com, Also follow our Instagram account to stay tuned.

    • @Pepcoding
      @Pepcoding  2 года назад

      Many many thanks. For better experience please explore nados.pepcoding.com
      Also don't forget to follow our Instagram account.

  • @VishalKumar-sm8bo
    @VishalKumar-sm8bo 3 года назад +6

    best solution ever. Thank you sir!!!!
    TC for this code is O(n)??

    • @Pepcoding
      @Pepcoding  3 года назад +1

      Thankyou beta,
      I am glad you liked it. I also hope that you are watching till end.
      If you like our efforts, will you like to write a few words about us here (www.quora.com/How-do-I-start-learning-or-strengthen-my-knowledge-of-data-structures-and-algorithms )

    • @adityagupta1805
      @adityagupta1805 3 года назад

      yes bro

    • @shoaibansari6702
      @shoaibansari6702 3 года назад

      I don't think so because within first for loop he is using while loop to increment j

    • @narayanbansal4036
      @narayanbansal4036 3 года назад +1

      If i == j is taken everytime in the loop then the TC = n*k in the worst case when the array is sorted
      If the other one is taken then it is linear

    • @KrishnaGupta-xd6xu
      @KrishnaGupta-xd6xu 3 года назад +2

      @@narayanbansal4036 bhai dhyan s dekh optimization if(j

  • @deepmala3970
    @deepmala3970 2 года назад

    this vid is from which
    playlist?

  • @dpxy1599
    @dpxy1599 Год назад

    thanz sir samajh me aa gaya

  • @anantjain1263
    @anantjain1263 4 года назад +2

    Sir aapne nge[j] < i+k kra h iski jgh
    nge[j]< nge[i+k] ni aana chaiye kyuki agar hmara j =1 h or nge[j] = 9 hua to
    9 < 1+4 is not true?

    • @amanaswal11
      @amanaswal11 4 года назад +5

      Brother! we are storing index in nge array.

  • @sakshigupta7616
    @sakshigupta7616 4 года назад +1

    Great explanation sir!!!

  • @44_shubhamsharma75
    @44_shubhamsharma75 4 года назад +1

    Accha explain Karte ho Sir!!

  • @adityagupta1805
    @adityagupta1805 3 года назад

    Epic level teaching

  • @shivammehta9661
    @shivammehta9661 3 года назад

    Very Nice Explanation Sir......Keep making videos

    • @Pepcoding
      @Pepcoding  3 года назад

      I am glad you liked it. I also hope that you are watching till end and trying to understand the what, how and especially why of the problem.
      If you like our efforts, we request a review
      g.page/Pepcoding/review?rc
      You can subscribe to our channel here
      ruclips.net/user/Pepcodingabout?view_as=subscriber

  • @KB-zg8ho
    @KB-zg8ho 4 года назад +2

    Sir this can be done using deque too in O(n)

    • @Pepcoding
      @Pepcoding  4 года назад +4

      Agreed. Also, that is easier. If you watch closely this is doing the same thing as Dequeue.

  • @bhaskarmishra8479
    @bhaskarmishra8479 2 года назад

    Great content!

  • @akshatsharma6631
    @akshatsharma6631 Год назад

    Thank you sir

  • @SMARTCLIQ
    @SMARTCLIQ 17 дней назад

    why these que time complexity is not n square

  • @jatinlodhi980
    @jatinlodhi980 2 года назад

    at line no. 21 , it should be st.push(arr[ arr.length-1]);

    • @Pepcoding
      @Pepcoding  2 года назад

      For better insight, visit nados.io, post your doubts, community will help you out there.

    • @kashba
      @kashba 2 года назад

      no, index comparision kar rahe hai isme

  • @mradulagrawal1579
    @mradulagrawal1579 2 года назад

    Wouldnt it be O(n^2) for cases like 1,2,3..n and k=n/2

    • @Pepcoding
      @Pepcoding  2 года назад

      For such queries sign up on nados.io
      You can post such queries on community tab, our alumni and mentors will guide you.

  • @adityachoudhary1739
    @adityachoudhary1739 4 года назад

    sir where is the link of that prerequisite video ?

  • @xtenza9065
    @xtenza9065 3 года назад

    Sir, how do we meditate over a question? If you can give a way for that then that would be great.

  • @santoshjoshi3396
    @santoshjoshi3396 2 года назад

    what about [1.-1] k=1;?

  • @AdityaTyagi-c1m
    @AdityaTyagi-c1m 2 месяца назад

    Line 36 you pushed i but not arr[I]

  • @biswarupdas9079
    @biswarupdas9079 3 года назад

    "If the numbers are in increasing order, you are essentially check each element in the window . Then time complexity will be same as brute force"...
    What to do in this case ,sir??
    please reply!!

  • @omkush3209
    @omkush3209 2 года назад +1

    This code is not working on leetcode problem -239

    • @CoderAdi
      @CoderAdi Год назад

      yes do you know the solution its showing tle

  • @karthikrashinkar204
    @karthikrashinkar204 3 года назад

    You look like rakshit shetty brother 😁
    by the way it was really good explanation 👌🏿

    • @Pepcoding
      @Pepcoding  3 года назад +2

      🤭 wow, this cheers me up. I am glad we at pepcoding could be of help to you. Keep learning. Also, recommend us to your juniors and peers, they may also benefit.

  • @ishanpatni2684
    @ishanpatni2684 4 года назад +1

    Sir pleaseeeeee upload the level up course further please

  • @AshutoshAnand-o5l
    @AshutoshAnand-o5l 7 месяцев назад

    Goddamn this guy!!!!
    best for a reason

  • @alpishjain1317
    @alpishjain1317 3 года назад +1

    Awesome video!

    • @Pepcoding
      @Pepcoding  3 года назад +1

      Thankyou beta!
      Keep learning and keep loving Pepcoding😊

  • @anujrawat6304
    @anujrawat6304 3 года назад

    if the array is in increasing order then j will jump to each window element which lead to n^2 time

    • @itzmanny8905
      @itzmanny8905 2 года назад

      Stocks ke value ka graph dekha hai kya kabhi

  • @urvishbundela7826
    @urvishbundela7826 2 года назад

    Just Wow❣

    • @Pepcoding
      @Pepcoding  2 года назад

      Glad to know that you love our video, for better experience.
      Visit - nados.pepcoding.com and sign up to NADOS.
      Don't forget to follow us on Instagram instagram.com/pepcoding/

  • @AshutoshSingh-se8vf
    @AshutoshSingh-se8vf 4 года назад

    Sir,what if j becomes greater than i,
    as you only write smaller than condition ..i.e-if(j

    • @Pepcoding
      @Pepcoding  4 года назад +1

      agar j == i ho gya to nge dobara calculate karenge.

    • @siddhantprakash.
      @siddhantprakash. 3 года назад

      I've the doubt..
      did you resolved the problem?

  • @gagantilak3118
    @gagantilak3118 4 года назад +1

    Sir, can we solve this question using reverse or maximum priority queue?

    • @Pepcoding
      @Pepcoding  4 года назад +1

      yes, complexity will be higher

  • @pranadeep_music4389
    @pranadeep_music4389 2 года назад

    behtareen

  • @Sachin95999
    @Sachin95999 2 года назад

    really good sir

    • @Pepcoding
      @Pepcoding  2 года назад

      Keep watching. sign up on nados.io for better experience and well organised content.

  • @NiteshKumar-do4en
    @NiteshKumar-do4en 4 года назад

    Sir you are great

  • @prashantnagrurkar2784
    @prashantnagrurkar2784 3 года назад

    Sir, jo video dekhne bole hai as a prerequisite, uska link bhi dijiye, 2:45

  • @bloody9162
    @bloody9162 2 года назад

    Superb!

    • @Pepcoding
      @Pepcoding  2 года назад

      Thanks a lot!
      For better experience and well-organised content
      visit- nados.pepcoding.com
      Also you can post your query on community tab.
      Don't forget to follow us on Instagram
      instagram.com/pepcoding/

  • @anjneykumarsingh4461
    @anjneykumarsingh4461 3 года назад +1

    Sir j peeche chut gya toh age lake barabar krdere, jb j age nikl jara tb ka?

    • @Pepcoding
      @Pepcoding  3 года назад

      Beta, I regret to inform you that, I won't be able to answer/solve the personal doubts of each and every student over here. For clearing your doubts, you can join our community on telegram - t.me/pepcoding.
      Also, we have a premium facility available for the students in which you can get the 12 hours doubt support facility. Jisme aap agr kisi bhi question main kahin bhi faste ho to aap doubt support par reach kar skte ho aur aapko TA assign ho jayega and you can get your doubt resolved from them.

    • @siddhantprakash.
      @siddhantprakash. 3 года назад

      same doubt i've.
      did you resolved it?

  • @anuragkumargupta7506
    @anuragkumargupta7506 3 года назад

    Hi sir , it is helpful if you provide the vedio link of the questions which is prerequsite for the current questions. or refference vedios

    • @vinamrasangal8436
      @vinamrasangal8436 Год назад

      Sometimes I feel very amazed, how a channel with such a awesome content have only reached 53k subs in last 3years,
      the team and specially Sumeet Sir deserves a minimum of 100k subs...hope they reach this milestone soon.

  • @ashishm8850
    @ashishm8850 3 года назад

    Thank you!

    • @Pepcoding
      @Pepcoding  3 года назад

      Keep learning and keep growing😊

  • @fashionvella730
    @fashionvella730 3 года назад

    PLEASE UPLOAD EASY TO ANALYZE INPUT

  • @syedhabeebuddin101
    @syedhabeebuddin101 3 года назад

    Thanks !

    • @Pepcoding
      @Pepcoding  3 года назад

      Keep learning, Keep growing and keep loving Pepcoding!😊

  • @ananyasingh1141
    @ananyasingh1141 3 года назад

    Sir, ismai agar next greater element window ke bahar hoota h toh uss i ke liye whi greatest element hai...woh j kaise detect kar raha idhar...konse part of code m aa raha woh nhi smj aaya...

    • @revaanmishra
      @revaanmishra 3 года назад

      thats why we're always doing j = i, because if for a window the NGE is outside then J will be at the correct position.

    • @siddhantprakash.
      @siddhantprakash. 3 года назад

      @@revaanmishra but we are doing that only when ji ?
      how to bring back that to i?

    • @kashba
      @kashba 2 года назад

      @@siddhantprakash. har loop chalne ke baad vo sliding window ke andar ya i se peche hoga
      agar peeche hai to humne same to i laane ka code krdiya
      aage hai to koi dikkat ki baat nahi hai cuz check bhi hume aage krna hai as sliding window goes ahead

  • @shivangsharma4873
    @shivangsharma4873 4 года назад

    Sir what is the time complexity of this solution and how can we optimize it??

    • @Pepcoding
      @Pepcoding  4 года назад +1

      Already optimized. O(n)

  • @RajatSingh-dg8ov
    @RajatSingh-dg8ov 3 года назад +3

    Sir aap badass ho bhaai!. Kya mast he kar dete ho.Please open a patreon, I will pay. Thanks

    • @Pepcoding
      @Pepcoding  3 года назад +5

      Haha..bro aage 5 saal baad jab apna band bnayunga aur nya youtube channel open kruga tab open kruga patreon😋

    • @Pepcoding
      @Pepcoding  3 года назад +3

      Bhai, 5-6 saal mei ek music band bnaunga. Uske channel pe patreon rahega.

    • @RajatSingh-dg8ov
      @RajatSingh-dg8ov 3 года назад

      @@Pepcoding 🤣🤣🤣🤣 sir aap logic mast clear Karne k Saath comedy b Kar dee hi, nice!

  • @AnkitAsatiM10
    @AnkitAsatiM10 3 года назад

    awesome!

  • @ayushgoel9584
    @ayushgoel9584 4 года назад +1

    sir, the solution which you have discussed is taking O(nk) time. ex- 1,2,3,4,5,6,7,8,9,10. for this j will jump 3 times for every window. i.e. , when array is sorted , time complexity will be o(nk). right??

    • @Pepcoding
      @Pepcoding  4 года назад +2

      Nope, it'll jump 3 times for 1st window and only 1 time for all subsequent windows. Print i and j in the inner loop for your example.

    • @ayushgoel9584
      @ayushgoel9584 4 года назад

      @@Pepcoding Ok sir, now i understood, thanks for the reply

    • @crackthecode1372
      @crackthecode1372 4 года назад

      @@Pepcoding so what would be the time complexity?

    • @codemaestro9904
      @codemaestro9904 4 года назад +2

      @@Pepcoding The 'if' condition instead of while to update 'j' prevents O(n*k), right ?

    • @Pepcoding
      @Pepcoding  4 года назад

      @@codemaestro9904 no, it is the fact that we jump don't just increase j by 1, instead we take it to nge

  • @rishabhgoyal2835
    @rishabhgoyal2835 4 года назад +1

    amazing : )

  • @atulbhaskar5919
    @atulbhaskar5919 3 года назад

    Sir howz the while is working in last FOR loop without incrementing j ? Line no 47

    • @Pepcoding
      @Pepcoding  3 года назад +1

      Beta, I regret to inform you that, I won't be able to answer/solve the personal doubts of each and every student over here. For clearing your doubts, you can join our community on telegram - t.me/pepcoding.

    • @faizan346
      @faizan346 3 года назад

      you need to check that again we are evidently jumping on next nge and placing our j on that pos. {j = nge[j]}

    • @itachiuchiha-vs3qb
      @itachiuchiha-vs3qb 3 года назад

      write down the code in paper and trace every iteration.. you will get it..

  • @girikgarg8
    @girikgarg8 Год назад

    Done!

  • @truegrabbers
    @truegrabbers 2 года назад

    woow

  • @panavkumar9009
    @panavkumar9009 2 года назад

    I was able to solve 99% question myself but missed that j < i condition due to which I got TLE on LC. Thanks for this explanation.

    • @HAMBIKASHARMA
      @HAMBIKASHARMA 2 года назад

      Bro last mei
      J=nge
      kyu Kiya hai?

    • @AYJ959
      @AYJ959 Год назад

      @@HAMBIKASHARMA jump krne lie j ko nay nge pr within window

  • @madhumitabhunia8937
    @madhumitabhunia8937 3 года назад

    One solution might be with using double ended queque🤔

  • @samirray7544
    @samirray7544 3 года назад +1

    Wow................ : ) : )

  • @crackthecode1372
    @crackthecode1372 4 года назад +1

    It is giving tle on ib😞

    • @Pepcoding
      @Pepcoding  4 года назад +1

      ek baar aapka solution post kijie. koshish karte hain. complexity iski O(n) he hai.

    • @rochishajaiswal1643
      @rochishajaiswal1643 4 года назад

      @@Pepcoding
      Sir this code is giving tle on gfg ide. Please help sir.
      import java.util.*;
      import java.lang.*;
      import java.io.*;
      class GFG {
      public static void main (String[] args) {
      //code
      Scanner sc=new Scanner(System.in);
      int t=sc.nextInt();
      for(int r=0;r

    • @45_ritiksharma32
      @45_ritiksharma32 2 года назад

      Giving tle on pepcoding portal as well

  • @anjneykumarsingh4461
    @anjneykumarsingh4461 3 года назад

    J=I shi h

  • @ayushsultania
    @ayushsultania 3 года назад

    for this solution it wouldnt work if input is
    10
    1 2 3 4 1 2 3 4 5 6
    4
    it gives tle

  • @LegitGamer2345
    @LegitGamer2345 4 года назад

    awesome !

    • @Pepcoding
      @Pepcoding  4 года назад

      Thank you! Cheers!

    • @LegitGamer2345
      @LegitGamer2345 4 года назад

      @@Pepcoding sir I have one doubt , time complexity of this is always O(N) right ?

    • @Pepcoding
      @Pepcoding  4 года назад

      Yes

    • @LegitGamer2345
      @LegitGamer2345 4 года назад

      @@Pepcoding okay sir :D