L5. Fruit Into Baskets | 2 Pointers and Sliding Window Playlist

Sdílet
Vložit
  • čas přidán 25. 03. 2024
  • Notes/Codes/Problem links under step 10 of A2Z DSA Course: takeuforward.org/strivers-a2z...
    Entire playlist: • Two Pointer and Slidin...
    Follow us on our other social media handles: linktr.ee/takeuforward

Komentáře • 69

  • @ArjunAR06
    @ArjunAR06 Před měsícem +18

    I'm visiting this problem after a week (I solved this by watching striver's solution). I was not able to solve a single problem on my own thinking. I saw few comments stating that they were able to solve this by themselves and I was like "Am I doing something wrong. Why am I not able to solve on my own?" To my surprise, I completed the entire sliding window playlist and when I revisited after like a week, I'm able to solve every single sliding window problem in like 20 minutes. You deserve a huge salute, Striver!

  • @Sandeep-tn8mz
    @Sandeep-tn8mz Před 3 měsíci +85

    This was asked to me in AMAZON

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

      You got selected??

    • @Sandeep-tn8mz
      @Sandeep-tn8mz Před 3 měsíci

      @@himanshu_047 NO bro .. Im not from CS background so, my CS fundamentasl are not strong.. especially OSI, System design.. I was able to solve the current problem but messed up in system design problem.

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

      @@himanshu_047 he's here so , you know..

    • @Shivam-ux8mn
      @Shivam-ux8mn Před 2 měsíci +2

      Bhaiya mai abhi class 12 pass kiya haii aur abhi btech cse krne ka decide kiya hu and in jee mains meri 73 percentile bani aur abhi mai drop lu ya na lu ke doubt mw hu , ek point ye kehta hai ki college se zada skills matter krti hai agar tumhare pass skills hai to tumhari placement off campus bhi ho jayegi but ek taraf man me ye bhi hai ki kya ek baar try krna chhaiye , is taking a drop is worth it?

    • @Sandeep-tn8mz
      @Sandeep-tn8mz Před 2 měsíci +11

      Hey bro ,
      At the end of the day your skills matter more than any college bro. Yes , the top tier colleges will give you that early career boost but its you who has to push yourself.
      Irrespective of college , if you keep yourself pushing you can do it from any college.
      yes you will have more difficulties , but they will definitely teach you a good lesson bro...
      ALL THE BEST
      (And if you are confident that you can do better by taking drop, go for it. You are still young, don't think much about outside noise. At the end of the day Its you who has to feed your family. )

  • @user-bf7yz3qd2i
    @user-bf7yz3qd2i Před 3 měsíci +11

    This was asked to me in Adobe.

  • @AkankshaGupta-zn7he
    @AkankshaGupta-zn7he Před 3 měsíci +11

    Just by watching & understanding the previous 4 videos, I was able to solve it on my own. First with brute then the sliding window technique. That's the magic of Striver😃❤

    • @jimit2795
      @jimit2795 Před měsícem

      how did you come to know that you have to use map or set in it.

    • @darkistheknight
      @darkistheknight Před 9 dny +1

      @@jimit2795 Look I am a noob myslef, but consider this, wherever we have to maintain something in terms of numbers like k distinct, at most k, you should know it is gonna be map. Here also we have two baskets i.e. we can store maximum of two types and we have to maximize the numbers of those 2 types of fruits. So at most K type questions. Hence map/set. That's all there's to it. Don't worry keep grinding. You'll get it.

    • @TheAditya-zt9pb
      @TheAditya-zt9pb Před 3 dny

      @@darkistheknight exactly your intuition is similar to mine

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

    althugh i have alredy did all these patterns questions stii here for your support ! thanks a lot for that dp and graph series and others too.

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

    You made my DSA journey easy

  • @raghavmanish24
    @raghavmanish24 Před 21 dnem

    hey striver you make things so simple , some time i got jealous that some others also can know this method from this videos ........thanku again striver

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

    I just solved before seeing this video by just copy paste the last code from L4 and did some tweaks. 🙌🙌

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

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

  • @selviraghavi-jp4ep
    @selviraghavi-jp4ep Před měsícem

    This question was asked in infosys specialist programmer role and bro made a video thankfully :)

  • @AtulSingh-dp8wo
    @AtulSingh-dp8wo Před 3 měsíci

    most awaited ❤thanks a lot!

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

    Striver bro you are a pro....Salute!

  • @AK-nj1je
    @AK-nj1je Před 5 dny

    Actually the space complexity of the most optimal approach will be almost O(n)
    Coz just imagine all the elements are different then, the code will not care what's the size of the map, it will just keep adding elements in the map, howeven the maxlen will not be affected.
    Amazing lecture!!!!

  • @sonakshibajpai6445
    @sonakshibajpai6445 Před 17 dny

    Thank you striver !!

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

    Understood bhaiya ❤

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

    This solution can also be optimised by in place of storing count of the character in map if we store last index of the character

  • @Abcd-jt1qs
    @Abcd-jt1qs Před 15 dny

    Understood sir! Thank you :)

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

    thanks striver

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

    thanks

  • @AJ-ve1yb
    @AJ-ve1yb Před 3 měsíci

    should i follow sequence of this playlist according to a2z sheet or to watch first which has been uploaded first @striver

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

    Understood sir!

  • @fizzgaming7910
    @fizzgaming7910 Před 13 dny

    Pointing out a small error, instead of erasing mpp[arr[l]] in the code, erase arr[l] and the code works flawlessly

  • @soumeshnayak4546
    @soumeshnayak4546 Před 23 dny +1

    I was unable to solve a single question in Sliding Window and two pointers after watching the above concepts I can think of and able to solve this question.
    This is the solution that I have solved on my own.
    def totalFruit(self, fruits: List[int]) -> int:
    maxlen=0
    map={}
    i=0
    for j in range(len(fruits)):
    map[fruits[j]]=map.get(fruits[j],0)+1
    while len(map)>2:
    map[fruits[i]]-=1
    if map[fruits[i]]==0:
    del map[fruits[i]]
    i+=1
    if len(map)

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

    thanks sir can you update in take you forward site as well

  • @nishantshrivastava8937

    Theoretically, we have optimised the solution to O(n) but the solution of O(2N) is faster on runtime while programming.

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

    understood

  • @Rathee_Fan
    @Rathee_Fan Před 3 měsíci +6

    28:58 I have a question bhaiya .. Let's take this example {1,2,1,2,1,2,1,2,3,4,5,6,7,8,9,10,11} .. if you see in case of optimal solution then SC=O(n/2) for map ... so how it is O(1) .
    And if you make an example of 10^5 length array the same as the upper example then also SC=O(10^5/2)..

  • @shreerangaraju1013
    @shreerangaraju1013 Před měsícem

    Can anyone tell me what iPad software is striver using?

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

    bahaiya please upload article on the website for this

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

    done

  • @txbankush4601
    @txbankush4601 Před 21 dnem

    can anyone provide me the full code because my some testcases are not being passed according to this pseudocode

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

    🙌🏻

  • @lucifee9407
    @lucifee9407 Před měsícem +1

    Striver if you are the interviewer ,you ask to optimize it to 2N to N ??

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

      mostly dont ask...9 out of 10

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

    😍

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

    ❤🙌🏻👍

  • @user-gk4zy6bk7l
    @user-gk4zy6bk7l Před měsícem

    god

  • @noone-fl1qb
    @noone-fl1qb Před 2 měsíci +1

    Can anyone tell me why this is 'nt working
    int findMaxFruits(vector &arr, int n) {
    int left = 0, right = 0, maxLength = 0;
    unordered_map map;
    while (right < arr.size()) {
    map[arr[right]]++;

    if (map.size() > n) {
    map[arr[left]]--;
    if (map[arr[left]] == 0) {
    map.erase(arr[left]);
    }
    left++;
    }
    if(map.size()

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

      Add "right++" after closing the bracket of "if(map.size()

    • @ayushisingh6638
      @ayushisingh6638 Před měsícem

      If this is geeksforgeesks question the varibale n is the size of array. if questio says 2 basket then you need to use 2 instead of n
      the following will produce correct result:
      int totalFruits(int n, vector &arr) {
      int left = 0, right = 0, maxLength = 0;
      unordered_map map;
      while (right < n) {
      map[arr[right]]++;
      if (map.size() > 2) {
      map[arr[left]]--;
      if (map[arr[left]] == 0) {
      map.erase(arr[left]);
      }
      left++;
      }
      maxLength = max(maxLength, right - left + 1);
      right++;
      }
      return maxLength;
      }

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

    Great explanation as usual. However the map is actually not necessary.
    int findMaxFruits(vector &arr, int n) {

    int fruit1 = arr[0],
    fruit2 = -1;
    int nextLeftPos = -1;
    int l = 0, r = 1;
    int count = 1;
    while (r < n)
    {
    if (arr[r] == fruit1 || arr[r] == fruit2)
    {
    if(arr[r] != arr[r-1]) nextLeftPos = r;
    count = max(count, r - l + 1);
    r++;
    }

    else if (fruit2 == -1)
    {
    fruit2 = arr[r];
    nextLeftPos = r;
    count = max(count, r - l + 1);
    r++;
    }
    else
    {
    l = nextLeftPos;
    fruit1 = arr[l]; fruit2 = arr[r];
    }
    }
    return count;
    }

  • @yashwanthkumar2513
    @yashwanthkumar2513 Před měsícem

    +1

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

    but i am happy with 0(2N) time complexity😂😂😂😂

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

    Please add C++ code also

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

    understood

  • @raghavmanish24
    @raghavmanish24 Před 21 dnem

    understood

  • @rlm3227
    @rlm3227 Před 27 dny

    understood