Sliding Window Maximum | Leetcode

Sdílet
Vložit
  • čas přidán 15. 07. 2021
  • Check our Website:
    In case you are thinking to buy courses, please check below:
    Link to get 20% additional Discount at Coding Ninjas: bit.ly/3wE5aHx
    Code "takeuforward" for 15% off at GFG: practice.geeksforgeeks.org/co...
    Code "takeuforward" for 20% off on sys-design: get.interviewready.io?_aff=takeuforward
    Crypto, I use the Wazirx app: wazirx.com/invite/xexnpc4u
    Take 750 rs free Amazon Stock from me: indmoney.onelink.me/RmHC/idje...
    Earn 100 rs by making a Grow Account for investing: app.groww.in/v3cO/8hu879t0
    Linkedin/Instagram/Telegram: linktr.ee/takeUforward
    ---------------------------------------------------------------------------------------------------------------------------------------------------- SDE sheet: bit.ly/takeUforward_SDE
    Watch at 1.25x for better experience ..
    ✅Use coupon-code "TAKEUFORWARD" for getting 15% for all CN courses: aff.codingninjas.com/click?o=...
    ---------------------------------------------------------------------------------------------------------------------------
    Problem Link: leetcode.com/problems/sliding...
    C++/Java Code: takeuforward.org/data-structu...
    ---------------------------------------------------------------------------------------------------------------------------
    ✅Use coupon-code "STRIVER" for getting 10% on all subscriptions of Unacademy
    ✅Use coupon-code "TAKEUFORWARD" for getting 10% for all GFG courses: bit.ly/tuf_gfgCourse​
    ✅Please Please SUBSKRIIIBEEEEE the new channel: / @striver_79
    ---------------------------------------------------------------------------------------------------------------------------
    Products that I use:
    Best Mic in Affordable Range: amzn.to/3hiTr9p
    Yeti mic: amzn.to/3dL4tDF
    Boya mic: amzn.to/3h4ROgy
    Logitech webcam: amzn.to/3h7d2KI
    Cooling pad: amzn.to/3y46pi4
    Wacom pad: amzn.to/3xacSIq
    Ring light: amzn.to/3y7aaU1
    Mic Arm Stand: amzn.to/3qzMuFa
    Digitek Green light stand: amzn.to/2U7B3bI
    Digitek Stand: amzn.to/363m7Oo
    Tripod: amzn.to/35ZmbyT
    Office chair: amzn.to/3qysu5Z
    Ipad Air: amzn.to/3hjpiXx
    Iphone 12: amzn.to/3AavyJV
    Watch: amzn.to/3hn7w5A
    Macbook Air: amzn.to/363lSTu
    Macbook Pro: amzn.to/3qCczDr
    Board: amzn.to/3x8GLIO
    Mouse: amzn.to/360Asv1
    ---------------------------------------------------------------------------------------------------------------------------
    ✅Placement Series: • Let's give back to the...
    ✅Placement Series (Arrays, Sorting..): • C++ and Java |Brute-Be...
    ✅Hashing Playlist: • Two Sum Problem | Leet...
    ✅Greedy Playlist: • N meetings In One Room...
    ✅Recursion Playlist: • L8. Combination Sum | ...
    ✅Graph Playlist: • 3 MAJOR ANNOUNCEMENTS ...
    ✅Two Pointer Playlist: • 3 SUM | Brute | Better...
    ✅Binary Search Playlist: • Nth Root of a Number U...
    ✅LinkedList Playlist: • Reverse a Linked List ...
    ✅Advanced DS playlist: • Fenwick Tree Tutorial ...
    ✅Stack&Queue Playlist: • Implementation of Stac...
    ✅Greedy Algorithms: • N meetings In One Room...
    ---------------------------------------------------------------------------------------------------------------------------
    If you appreciate the channel's work, you can join the family: bit.ly/joinFamily​
    ✅Thumbnail Creator: / rikonakhuli
    ✅ Striver's Linkedin Profile: / ​
    ✅ Instagram: / ​
    ✅Connect with us: www.google.com/search?client=... (Use Link in Mobile only, if not works search "takeUforward" in telegram)..
    ##dsa​ #striver #leetcode

Komentáře • 212

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

    If you understand it, please do like it, and if possible please spare 2 seconds for a comment :)

    • @anoopraj9319
      @anoopraj9319 Před 2 lety

      make videos on dp please

    • @JohnWick-kh7ow
      @JohnWick-kh7ow Před 2 lety

      What's the difference between deque and list?

    • @PrinceKumar-el7ob
      @PrinceKumar-el7ob Před 2 lety +5

      16:22 smaller than equal to is not ok!!
      only smaller than will be ok striver.
      i don't know why anyone didn't point this out.

    • @pranayjain._
      @pranayjain._ Před 11 měsíci

      @@PrinceKumar-el7ob It would work. Try some test cases.

    • @artofwrick
      @artofwrick Před 5 měsíci

      The best explanation on sliding window

  • @zetro6311
    @zetro6311 Před 2 lety +88

    I got this question in a Microsoft interview for SDE 1

    • @kingmaker9082
      @kingmaker9082 Před rokem

      Is it offcampus or oncampus

    • @zetro6311
      @zetro6311 Před rokem

      @@kingmaker9082 Off campus

    • @kingmaker9082
      @kingmaker9082 Před rokem +1

      @@zetro6311 from where u applied for Microsoft or have u got any referral? Plz guide me...btw what is the result of interview bro?

    • @vinyass3733
      @vinyass3733 Před rokem

      @@kingmaker9082 hello bro? which batch are you?

    • @kingmaker9082
      @kingmaker9082 Před rokem

      @@vinyass3733 23 batch

  • @priyanshityagi8313
    @priyanshityagi8313 Před 2 lety +75

    This explanation literally made me overcome my fear of stacks and queues. Thank you so much!

    • @ayushrawat7951
      @ayushrawat7951 Před rokem

      Ya exactly..... I also have fear of stack and queue.. Even though they are quite easy to understand.

    • @purushottam108
      @purushottam108 Před 25 dny

      How one question can over come your fear? 🤔🤔🤔🤔

  • @parthsalat
    @parthsalat Před rokem +8

    I couldn't understand this problem's solution even after reading several leetcode discuss posts. However, I understood this completely at once after watching your video! Thanks!

  • @koushalsharma7041
    @koushalsharma7041 Před 2 lety +5

    It is always challenging as well as fun to learn the optimal solution. But, your explanation helps a lot to understand it better.

  • @harshexploring4922
    @harshexploring4922 Před 2 lety +11

    kudos to your patience while explaining sir.The way of you instruct the solution to the problem was quite appealing, unlike some other youtubers who just write the code in rush without explaining properly.
    Thanks again

  • @spardha-yug
    @spardha-yug Před rokem

    THank you bro.
    Brute force+Optimized+code explanation---->complete video.
    Thanks a ton.

  • @AbhishekKumar-hh8xc
    @AbhishekKumar-hh8xc Před 2 lety +2

    Good explanation. That's an art which is rare. Every one knows the steps and code and solution, But very few know the explanation and intution

  • @deepikasrivastava3040
    @deepikasrivastava3040 Před 2 lety +4

    Best Explanation. Thank you so much. Finally understood the use of deque for this problem.

  • @raghav5354
    @raghav5354 Před rokem +4

    I don’t get the intuition that whenever we remove leftmost index from our window to go to the next window, why the left most index will also be the left most element in the deque (so we just remove the first element from our deque). In my thinking, I should loop through the deque to find the index to remove for every window.

  • @nayan_CSE
    @nayan_CSE Před 11 měsíci +1

    What a explanation 😯...I think this is the best video of window problem in CZcams... Thank you so much for this great video....

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

    Can't find a better explanation than this for a hard problem.. simple love it

  • @pb_yt992
    @pb_yt992 Před 2 lety +5

    Such a Nice Explanation even a beginner can understand. Thanks a lot

  • @AlokKumar-or8jz
    @AlokKumar-or8jz Před 10 měsíci +2

    easiest solution by naiveApproach
    vector maxSlidingWindow(vector arr,int k){//TLE
    vector ans;
    int n= arr.size();
    for (int i = 0; i < n-k+1; i++)
    {
    ans.push_back(*max_element(arr.begin()+i,arr.begin()+i+k)); // T.C = O(n) max_element
    }
    return ans;


    }

  • @AyushGupta-kp9xf
    @AyushGupta-kp9xf Před 2 lety +2

    Thanks man I was doubtful about the On complexity. your explanation cleared it :)

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

    Very good explaination , though i have to see it 2 times to completely understand the concept👍

  • @aryanchauhan8897
    @aryanchauhan8897 Před 3 lety +8

    Very great explanation. Thank you bhaiya for helping out the students but I have one suggestion pls upload videos on regular basis. I know u are very busy but it will be helpful for students who are appearing in this placement season. We have found the solution of dse sheet from anywhere but the video explanation of yours is very helpful to remember the concepts and do similar type questions on particular algorithms.

  • @thatbadguy2899
    @thatbadguy2899 Před 3 lety +25

    Please increase the frequency of uploads as this is PLACEMENT SEASON

  • @juanceja899
    @juanceja899 Před 2 lety

    I just had an interview with Google today and this was the algorithm I was given. That's why I'm here Haha. I was able to do it, but I took a different approach.

  • @patelchintu6492
    @patelchintu6492 Před 3 lety +33

    Waiting for tree series :)

  • @afaqueahmad7117
    @afaqueahmad7117 Před rokem

    Such an easy explanation, you make it look simple, love it!

  • @paragroy5359
    @paragroy5359 Před 2 lety

    Nice explanation your videos are really good...please keep on making such videos...you are doing a great job.

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

    This can also be solved using multiset, can we use multiset in the interview bhaiya?

  • @cinime
    @cinime Před rokem

    Understood! Amazing explanation as always, thank you very much!!

  • @kotapatiswaraj06
    @kotapatiswaraj06 Před 11 měsíci

    Great Explanation ! Thanks for the free quality content

  • @manask8116
    @manask8116 Před 8 měsíci

    you can do by using a multiset as well , it takes nlogn but its very simple to write the code

  • @oqant0424
    @oqant0424 Před 2 lety

    what an Explanation...........hats off ✌✌

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

    at 3:48 , deque should be Double ended queues I guess and not doubly linked list , btw nice explanation :)

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

      I think he meant that double-ended queues are typically implemented using doubly linked lists

    • @harshexploring4922
      @harshexploring4922 Před 2 lety

      @@alokesh985 if i am not wrong in doubly linked list one can access,insert and pop elements at any position but in deque one can do it only at front and back

  • @vrajsoni7610
    @vrajsoni7610 Před 3 lety

    Was waiting for a video on this specific question, thanks a lot

  • @sujalgupta6100
    @sujalgupta6100 Před rokem

    this was very hard for me . thanks for such a good explanation.

  • @anandyadav_
    @anandyadav_ Před 3 lety

    I just like every video of yours becuz your's are really gona help.

  • @Luk3Stein
    @Luk3Stein Před rokem

    Godly explanation. Suddenly, this problem does not look Leetcode Hard level

  • @bharatmehta30
    @bharatmehta30 Před 2 lety

    dude!! your explanation is amazing.

  • @anutoshghosh7893
    @anutoshghosh7893 Před 5 měsíci

    Is there any algorithm related to next greatest element or next smallest element, like how to devise algorithms for these type of problems, how to know we need a deque in this case as we do operations on both sides here?

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

    This question can also be solve using heap

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

    Thanks a lot! this was super helpful :)

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

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

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

    That definitely not at 5:15 reminds me of Dhoni xD.

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

    great explaination as always.... waiting for more videos

  • @jaydalsaniya6986
    @jaydalsaniya6986 Před 2 lety

    Worst case occurs when k = 1 , Inserting all N element and N-1 elements will be removed. This is because for each array element two operation DELETION -> INSERTION IS PERFORMED .

  • @sushantchoudhary444
    @sushantchoudhary444 Před rokem +1

    you are amazing God bless you. I wanna become like you one day

  • @biswaranjanbarik4052
    @biswaranjanbarik4052 Před rokem

    Ur explanation is so awesome.. It's really helpful to us..🙏
    can you please upload the video of 862. Leetcode problem Shortest subarray with Sum at Least K.🙏🙏
    please sir please I'm really stuck in this problem.

  • @vuyyurishanmukhaabhinavvar4096

    Bhai when your course starts in unacademy pls update waiting for your course

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

    Bro can u please make a video on how to deal with large numbers and what exactly 10^9+7.

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

    Hello Striver, new here, can you tell whether you are covering only this SDE sheet here on any other topics too ? anyone ?

  • @_-6912
    @_-6912 Před 3 lety +1

    I understood the solution!

  • @171-supratiksantra5
    @171-supratiksantra5 Před 3 lety

    I got this question in one interview round and also in the written round

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

    bhaiya please bring more videos fast fast .. placements are starting!!

  • @helderfilho4724
    @helderfilho4724 Před 9 měsíci

    Thank You very much! You explain very well.

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

    Great Explanation

  • @sohailshaik9
    @sohailshaik9 Před rokem

    had to watch 2 times to understand this but it was beautiful. thanks

  • @kritikajain6677
    @kritikajain6677 Před 2 lety

    Great explanation!

  • @iamnoob7593
    @iamnoob7593 Před rokem

    Understood Thanks Striver , I am suffering from health issues , Still not giving up.

  • @1qwertyuiop1000
    @1qwertyuiop1000 Před 10 měsíci

    How about using Max Heap with size K.. time complexity will be NLogN i guess.. Space complexity will be O(K)

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

    Nice explanation bro!

  • @RamKrishna-ou6li
    @RamKrishna-ou6li Před 2 lety

    good explanation thank you so much

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

    how is i-k the outbound element? at 16:02 ?

  • @Code__madad
    @Code__madad Před rokem +1

    it is not working for all the test cases in geeks for geeks

  • @hussainkanchwala968
    @hussainkanchwala968 Před 5 měsíci

    very clear explanation, thanks

  • @gandhijainamgunvantkumar6783

    Bhaiya please make a video on the question: Find maximum of minimum for all the window sizes.

  • @mugambo5505
    @mugambo5505 Před 2 lety

    awesome explanation bro.
    all doubts gone

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

    the flow of teaching is amazing bor

  • @millionaires1
    @millionaires1 Před 2 lety

    Very good explanation, thank you bro

  • @anirudhvermarr
    @anirudhvermarr Před 4 měsíci

    Simply, thank you!

  • @prachigupta9713
    @prachigupta9713 Před rokem

    Thankyou So much for such excellent explaination

  • @1murkeybadmayn
    @1murkeybadmayn Před 9 měsíci

    The part that confuses me is dq.empty() or dq.front(), i thought they return a value at the index not the index itself. So how is it used here that it is checking the index and not the value? I am so confused.

  • @biswaranjanbarik4052
    @biswaranjanbarik4052 Před rokem

    Ur explanation is so awesome.. It's really helpful to us..🙏
    can you plzz upload the video of 862. Leeetcode problem Shortest Subarray with Sum at Least K.🙏🙏
    please sir please I'm really stuck in this prblm.

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

    Please add heap questions in SDE sheet.

  • @asp1859
    @asp1859 Před 8 měsíci

    Is the time complexity , really O(n) here ??.... because, if we take example, 6,4,5,3,2 with window size k =3, then once the greatest element 6 is removed, the next greatest element that is 5 should be placed at top, and for doing that worst case, there will be (k-1) comparisons. Anybody please correct me if I am wrong .

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

    can we use max heap??

  • @siddhantraut4773
    @siddhantraut4773 Před 2 lety

    Great explanation ❤️❤️

  • @_dion_
    @_dion_ Před 5 měsíci

    the best explanation on yt.

  • @sahilsingh5695
    @sahilsingh5695 Před rokem

    not able to understand the lst part of the i>-=k-1 onwards part

  • @herculean6748
    @herculean6748 Před rokem

    we can do this using queue also, why queue is not used and deque is used?

  • @lifegood6401
    @lifegood6401 Před 3 lety +11

    Waiting for tree series eagerly.
    Placement already started in college, and tree , dp, strings remaining from the sheet

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

    *Brute force*
    class Solution {
    public:
    vector maxSlidingWindow(vector& nums, int k) {
    vectorans;
    int n = nums.size();
    for(int i=0; i

  • @ujjawaldewangan8345
    @ujjawaldewangan8345 Před 3 lety

    It is necessary because no one teach us like you

  • @s41nt2
    @s41nt2 Před rokem

    Genius guy🙌🙌

  • @shreyashachoudhary480
    @shreyashachoudhary480 Před 2 lety

    Thanks!

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

    Great brother 👌👌 keep it up 👆

  • @user-gq6sq8zk8s
    @user-gq6sq8zk8s Před 6 měsíci +1

    Thank You!!

  • @yashsaini9494
    @yashsaini9494 Před rokem

    This person is gifted

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

    Huge fan of your videos

  • @kapilpatel9379
    @kapilpatel9379 Před rokem

    Could not understand completely, but will come back

  • @sudiksha586
    @sudiksha586 Před 11 měsíci

    thank you striver!

  • @SuryajyotiDas
    @SuryajyotiDas Před rokem

    amazing brother

  • @kshitijmishra2716
    @kshitijmishra2716 Před 2 lety

    why we should not directly give optimised solution of the particular problem in the interview

  • @511sanjeevthalod7
    @511sanjeevthalod7 Před 3 lety +1

    Bro please make a video on your jee journey

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

    directly jumped to solution,
    Intution is missing... :(

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

    great explanation bhai

  • @itz_me_imraan02
    @itz_me_imraan02 Před rokem

    I solved it using segment tree

  • @soubhikghosh6564
    @soubhikghosh6564 Před 2 lety

    Too good an explanation

  • @Anonymous-uj3jx
    @Anonymous-uj3jx Před 2 lety

    understood thanks :)

  • @Yogeshyadav-iu4zk
    @Yogeshyadav-iu4zk Před 2 lety

    thanks brother

  • @krishnavamsichinnapareddy

    Nice explanation 👍

  • @shubham6215
    @shubham6215 Před 3 lety

    Thank you bhaiya ❤️👍

  • @RAHULROY-sb9nt
    @RAHULROY-sb9nt Před rokem

    videos are priceless.

  • @adeebarif3791
    @adeebarif3791 Před 2 lety

    👍 Great Bhaiya

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

    A day before the interview at 2x speed.

  • @abhityagi9807
    @abhityagi9807 Před rokem

    14:22 ye time complexity O(n-k)+O(k) ni honi chahiye be because hum ans vector bhi to store kar rahe hai plz clear my doubt