BS-12. Koko Eating Bananas

แชร์
ฝัง
  • เผยแพร่เมื่อ 20 มิ.ย. 2023
  • Problem Link: bit.ly/3CmCKVI
    Notes/C++/Java/Python codes: takeuforward.org/binary-searc...
    We have solved the problem, and we have gone from brute force and ended with the most optimal solution. Every approach's code has been written in the video itself. Also, we have covered the algorithm with intuition.
    Full Course: bit.ly/tufA2ZYt
    You can follow me across social media, all my handles are below:
    Linkedin/Instagram/Telegram: linktr.ee/takeUforward
    0:00 Introduction of Course

ความคิดเห็น • 237

  • @--Blood--Prince--
    @--Blood--Prince-- ปีที่แล้ว +144

    I see Koko is on a high carb diet😀😅

    • @prernagupta7729
      @prernagupta7729 9 หลายเดือนก่อน +1

      😂

    • @SreeCharan-dx7oc
      @SreeCharan-dx7oc 4 หลายเดือนก่อน +1

      🤣🤣

    • @dumpster-jackson
      @dumpster-jackson 29 วันที่ผ่านมา +3

      Gay

    • @omkarshendge5438
      @omkarshendge5438 22 วันที่ผ่านมา

      lmao but tbh koko is a monkey i think thats why he is eating bananas!

    • @kumarvansh007
      @kumarvansh007 10 วันที่ผ่านมา +1

      @@dumpster-jackson wtf

  • @vm1662
    @vm1662 10 หลายเดือนก่อน +57

    Great tip! Whenever there is a possible range of answers then we can apply binary search.. Thanks a lot Striver!

  • @pradyumnmulegaon385
    @pradyumnmulegaon385 7 หลายเดือนก่อน +63

    for those who are solving in leetcode the TotalH should be long long and also the CalculatetotalHours should also be long long type otherwise it will throw an error .....
    hope it is helpful

    • @socify4410
      @socify4410 7 หลายเดือนก่อน +7

      it was throwing run time error earlier. i thought i must be missing some edge case . after this change it got submitted. thanks to you buddy

    • @tanishagarwal5992
      @tanishagarwal5992 7 หลายเดือนก่อน +1

      Why to calculate total hours just check if it any instance the req time crosses h... Return false

    • @user-sm7zo5zd9t
      @user-sm7zo5zd9t 4 หลายเดือนก่อน +3

      When I was submitting my code, I encountered the same problem, Thanks buddy.

    • @tusharkhatri2921
      @tusharkhatri2921 4 หลายเดือนก่อน +1

      Bro on LeetCode not working after 121 test cases.

    • @ranjeet_sohanpal
      @ranjeet_sohanpal 3 หลายเดือนก่อน +2

      @@tusharkhatri2921 you can make the condition in the calculation of hour .When the time taken exceeds h, you immediately return that val and prevent the overflow

  • @mathguy198
    @mathguy198 ปีที่แล้ว +15

    Thanks Striver, always struggled to understand the reason behind the answer being always the low value, now it's clear as crystal, all thanks to you❤❤

  • @user-eh9wd8hp4f
    @user-eh9wd8hp4f ปีที่แล้ว +7

    never thought we could even solve this by binary search mind-blowing

  • @sauravchandra10
    @sauravchandra10 ปีที่แล้ว +29

    I remember doing this question earlier but the way you taught I am confident I can now easily solve problems like these in interviews with a clear and intuitive explanation. Understood, thanks!

    • @keshav9541
      @keshav9541 2 หลายเดือนก่อน

      same

  • @SuvradipDasPhotographyOfficial
    @SuvradipDasPhotographyOfficial ปีที่แล้ว +3

    The wait is finally over.... Thanks a ton striver❤

  • @visheshagrawal8676
    @visheshagrawal8676 ปีที่แล้ว +7

    I have earlier solved the question but intuition of yours is truly amazing... Great Content 🔥🔥

  • @CrazyHunk14
    @CrazyHunk14 5 หลายเดือนก่อน +1

    thanks to your intuition I could do this question on my own using the same approach that you explained in this video without any help! kudos

  • @abhaysinghrathore3192
    @abhaysinghrathore3192 9 หลายเดือนก่อน +4

    give this man the best teacher award in the world

    • @30sunique78
      @30sunique78 4 หลายเดือนก่อน

      In DSA sure is this best For development check chai aur code once

  • @cinime
    @cinime ปีที่แล้ว +1

    Understood! Wonderful explanation as always, thank you very very much for your effort!!

  • @thenikhildaiya.
    @thenikhildaiya. ปีที่แล้ว +18

    I just saw the question explanation and did the brute force and binary search approach all by myself just because of your previous videos explanation. Thank you so much for this wonderful and guided course.

    • @kumarshivam3661
      @kumarshivam3661 ปีที่แล้ว +1

      Bro please post the solution of code

    • @thenikhildaiya.
      @thenikhildaiya. ปีที่แล้ว +1

      @@kumarshivam3661 ** Brute Force :
      class Solution {
      public:
      int minEatingSpeed(vector& piles, int h) {

      int maxHours = -1 , k = 0 ;
      for(int i = 0 ; i < piles.size() ; i++)
      if(piles[i] > maxHours)
      maxHours = piles[i] ;
      for(int i = 1 ; i

    • @nostalgiccringeallhailchel3881
      @nostalgiccringeallhailchel3881 ปีที่แล้ว +1

      how far have you progressed in 3 weeks bro pls update

    • @thenikhildaiya.
      @thenikhildaiya. ปีที่แล้ว

      @@nostalgiccringeallhailchel3881 I did aggressive cows at last. Not good progress at all because of college exams🥲
      Just revising old linked list questions I did in past.

    • @nostalgiccringeallhailchel3881
      @nostalgiccringeallhailchel3881 ปีที่แล้ว

      @@thenikhildaiya. Which topic is aggressive cows in?

  • @ritiksoni5255
    @ritiksoni5255 3 หลายเดือนก่อน

    Thankyou you so much finally able to understand the approach and logics for solving DSA!!

  • @Dipanshutripathi2407
    @Dipanshutripathi2407 10 หลายเดือนก่อน

    I have no doubts after watching ur videos.

  • @akashdutta1620
    @akashdutta1620 7 หลายเดือนก่อน +4

    this was asked in an interview I came up with this solution without solving this before.. :)

  • @user-ti3bd8mp1w
    @user-ti3bd8mp1w ปีที่แล้ว +1

    understood
    Thank you striver for such an amazing explanation..

  • @knowthrvdo
    @knowthrvdo 5 หลายเดือนก่อน

    logic is same as finding first occurance THANK YOU

  • @RaviRavi-kt9gt
    @RaviRavi-kt9gt ปีที่แล้ว

    You are just incredible ❤️🎉🎉

  • @hareshnayak7302
    @hareshnayak7302 3 หลายเดือนก่อน

    Understood, Thanks striver for this amazing video.

  • @varun1017
    @varun1017 5 หลายเดือนก่อน +4

    use long long to calculateTotalHours instead of int to avoid overflow

  • @Surya1prakash
    @Surya1prakash ปีที่แล้ว +1

    Really amazing explanation 👏

  • @manavsingh5919
    @manavsingh5919 11 หลายเดือนก่อน

    Thank you striver....Understood everything🙂...keep up the good work

  • @AyushSharma-sd1ny
    @AyushSharma-sd1ny ปีที่แล้ว

    Great explanation Striver

  • @Amanali-rl9hw
    @Amanali-rl9hw ปีที่แล้ว +1

    finally found when to return the low and when to return the value high

  • @joeljacob4685
    @joeljacob4685 10 หลายเดือนก่อน

    Thanks so much!! I was able to solve it without watching the video !!

  • @shashwatkumar6965
    @shashwatkumar6965 5 หลายเดือนก่อน

    Great video! Just wanted to add one thing, in the question's Constraints it's given that n

    • @sukhii0220
      @sukhii0220 หลายเดือนก่อน

      just include the overflow condition in finding the hrs in the loop only if totalH >H break;

  • @AnmolGupta-oj4lm
    @AnmolGupta-oj4lm 11 หลายเดือนก่อน

    Understood Very Well!

  • @Jus-chill
    @Jus-chill ปีที่แล้ว +1

    Understood
    Please do upload videos consistently 😊

  • @ShivamMaurya-fq6ws
    @ShivamMaurya-fq6ws 9 หลายเดือนก่อน

    @striver thanks for explanation

  • @abhaythakur2597
    @abhaythakur2597 ปีที่แล้ว

    very helpful explaination

  • @ayushjaiswal7298
    @ayushjaiswal7298 ปีที่แล้ว

    Best explanation 🔥

  • @Yahsaswi
    @Yahsaswi ปีที่แล้ว

    was waiting for this

  • @akshaykeswani3601
    @akshaykeswani3601 ปีที่แล้ว

    Really Amazing👏👏👏

  • @NazeerBashaShaik
    @NazeerBashaShaik 3 หลายเดือนก่อน

    Understood, thank you.

  • @AdityaYadav-qf9qc
    @AdityaYadav-qf9qc ปีที่แล้ว

    Best explaination😍

  • @user-ny6tz2rb1r
    @user-ny6tz2rb1r 8 หลายเดือนก่อน +1

    Could you also please explain.Find K close elements with binary search.I've seen in some of the binary search approaches.Few of the solutions are with right=mid or left=mid.

  • @engineerssolutionpoint95
    @engineerssolutionpoint95 ปีที่แล้ว

    Nice explanation ❤

  • @visheshagrawal8676
    @visheshagrawal8676 ปีที่แล้ว +2

    If anyone was getting runtime error of integer overflow do change the datatype of totalhours to double or long long

  • @aaranyaksantra9933
    @aaranyaksantra9933 หลายเดือนก่อน

    nice explaination

  • @dayashankarlakhotia4943
    @dayashankarlakhotia4943 ปีที่แล้ว

    Good explanation understood

  • @neelnsoni1313
    @neelnsoni1313 11 หลายเดือนก่อน

    Thank You

  • @ravisingla9032
    @ravisingla9032 ปีที่แล้ว

    Was Waiting for the same

  • @harshitjaiswal9439
    @harshitjaiswal9439 ปีที่แล้ว +1

    Understood!

  • @aniketgupta8064
    @aniketgupta8064 ปีที่แล้ว +18

    hey striver, I feel that there is some problem with the leetcode question. for the testcase [805306368, 805306368, 805306368] and h = 1000000000, the expected answer is k=3
    when we use an ans variable to store the desired value of mid, the code gives output = 1 which is incorrect, however, when we simply return the low, the testcase passes.
    I am not able to understand what is happening here. If this is a case of an overflow, I have already substitued long long. Pls help.

    • @thegame587
      @thegame587 ปีที่แล้ว +13

      bool check(vector& piles,int h,int k){
      long long ans=0;
      for(int i=0;ih)return false;
      }
      //cout

    • @aniketgupta8064
      @aniketgupta8064 ปีที่แล้ว +3

      @@thegame587 Thanks a lot buddy for replying, it's working now. I had figured the overflow problem :)

    • @jayant-baid
      @jayant-baid ปีที่แล้ว +8

      @@aniketgupta8064 you can also make the func dataype, totalH datatype to long, this can also works

    • @aniketgupta8064
      @aniketgupta8064 ปีที่แล้ว +2

      @@jayant-baid thanks 👍

    • @MYMIND252
      @MYMIND252 9 หลายเดือนก่อน +2

      @@jayant-baid thank you bro i stuck in this problem for more than hour and you save me from it 😊😊

  • @impalash_ag
    @impalash_ag 10 วันที่ผ่านมา

    Hi Raj,
    Both the BF and Optimal solutions will have an additional TC of O(n) to find the maximum element from the array.

  • @YourCodeVerse
    @YourCodeVerse 7 หลายเดือนก่อน

    Understood✅🔥🔥

  • @culeforever5408
    @culeforever5408 9 หลายเดือนก่อน +1

    understood 😇

  • @senseiAree
    @senseiAree 9 หลายเดือนก่อน

    Understood ❤

  • @tanishkarawat5266
    @tanishkarawat5266 ปีที่แล้ว +1

    One doubt:
    what if h = 5 and piles = [30,11,23,4,20]here if we take k greater than 30 then also we'll get the answer. Then how can we apply that brute force where we are taking range of k to be [1, greatest_element] in that array?

  • @user-cb8ei9xp2b
    @user-cb8ei9xp2b 10 หลายเดือนก่อน

    while finding maximum element in an unsorted array it takes O(n) time so ultimately the time complexity of this question is O(n).

    • @prthmsh2930
      @prthmsh2930 6 หลายเดือนก่อน

      O(n*log(max) >>> O(n) :)

  • @happylearning6476
    @happylearning6476 7 หลายเดือนก่อน

    Should we not consider the + O(n) time to find the max element in the time complexity? Though i does not add to the over all complexity since we take the high value, still asking

  • @aritramallik8173
    @aritramallik8173 ปีที่แล้ว

    Dada tumi best❤❤❤

  • @kiranmoura2974
    @kiranmoura2974 ปีที่แล้ว

    9:54 at this while calculating time complexity of linear approach func will run for n times .but inside it ceil function will be used jiski time complexity log n hoti h so vo consider nahi krenge?

  • @user-tk2vg5jt3l
    @user-tk2vg5jt3l 5 หลายเดือนก่อน

    Thank you bhaiya

  • @pentapreethika4901
    @pentapreethika4901 7 หลายเดือนก่อน

    can we do this by finding sum of elements in array and getting the ceil value by dividing it with number of hours which takes o(N) time complexity which is better than binary search.

  • @srushtibhagchandani7255
    @srushtibhagchandani7255 หลายเดือนก่อน

    Understood!!

  • @Anony.musk01
    @Anony.musk01 ปีที่แล้ว

    understoood!!!

  • @Manishgupta200
    @Manishgupta200 ปีที่แล้ว +2

    Rather than using ceil we can use "sum = sum + (v[i] - 1)/ mid + 1;"
    It's more optimal

    • @shikharjoshi2998
      @shikharjoshi2998 8 หลายเดือนก่อน

      what's the logic behind the (v[i]-1) ??

    • @HemanthD-bf4he
      @HemanthD-bf4he 4 หลายเดือนก่อน

      Can you explain the logic?

  • @user-eb4ox5zv5m
    @user-eb4ox5zv5m 2 หลายเดือนก่อน

    To reduce runtime one can use instead of ceil
    ans += (piles[i]+k-1)/k;

  • @sateeshkumar1634
    @sateeshkumar1634 5 หลายเดือนก่อน +1

    Hey striver, i am getting 1 test case wrong in this question, don't know why
    i still tried with your own code , but it is not working, nearly 3 problems have been in this way,
    any one please respond....

  • @hitmanop4078
    @hitmanop4078 หลายเดือนก่อน +1

    Someone please give a advice like how can u build a logic, how to revice sometimes I just go all blank even in some easy question what should I do ??.

  • @pritivarse2129
    @pritivarse2129 ปีที่แล้ว +1

    Sir plz make videos on linked list and string part from A to Z dsa sheet

  • @ayushgoel01
    @ayushgoel01 11 หลายเดือนก่อน +4

    🔴Leetcode Solution:-
    class Solution {
    private:
    int maxi(vector arr){
    int ans = INT_MIN;
    for(int i=0; i

  • @jhanavikumari274
    @jhanavikumari274 วันที่ผ่านมา

    it is actually similar to finding the smallest divisor than threshold question literally

  • @yashendrabadal4776
    @yashendrabadal4776 หลายเดือนก่อน +1

    Koko for real is gonna shit the whole next day ! AWESOME VID STRIVER

  • @srinathv1412
    @srinathv1412 5 หลายเดือนก่อน

    Understood!!!!!

  • @amaankhan9845
    @amaankhan9845 ปีที่แล้ว

    Following this series. Really helpfull 💪💪

  • @deeptidip9864
    @deeptidip9864 ปีที่แล้ว

    Understood:)

  • @pallabghosh3840
    @pallabghosh3840 2 หลายเดือนก่อน

    great

  • @chethanprabhu4475
    @chethanprabhu4475 13 วันที่ผ่านมา

    Should we not add the O(n) to time complexity to find the max element in the array

  • @user-nk1mb5fy7j
    @user-nk1mb5fy7j ปีที่แล้ว

    UNDERSTOOD

  • @harshilpatel3205
    @harshilpatel3205 5 หลายเดือนก่อน

    Understood sir 🫡🤍

  • @Donquixote-Rosinante
    @Donquixote-Rosinante 7 หลายเดือนก่อน

    why this solution still falls in binary search. where we create extra memory for lowest pile 1 to highest pile 11 to get maximum pile per hour?

  • @ankithac5520
    @ankithac5520 ปีที่แล้ว

    Understood

  • @per.seus._
    @per.seus._ 11 หลายเดือนก่อน

    understood

  • @pihus3498
    @pihus3498 ปีที่แล้ว

    understood :)

  • @Thescienceworld652
    @Thescienceworld652 11 หลายเดือนก่อน +1

    i was doing this wrong for three hours and what i was doing wrong was only not applying double🙂

  • @VishalGupta-xw2rp
    @VishalGupta-xw2rp 10 หลายเดือนก่อน

    16:50 Low opposite polarity

  • @girikgarg8
    @girikgarg8 10 หลายเดือนก่อน

    Done

  • @23cash86
    @23cash86 ปีที่แล้ว

    Thanks++

  • @ziggywiggys
    @ziggywiggys หลายเดือนก่อน

    Why does it not work if I take the minimum eating speed as the minimum of the array ?

  • @vatsalyasharma5585
    @vatsalyasharma5585 ปีที่แล้ว

    By what time, will the whole A2Z course will be completed?

  • @RHuL706
    @RHuL706 ปีที่แล้ว +1

    Hi striver, i am experience developer approx 5 years, my query is how to get calls from recruiters

  • @shashankvashishtha4454
    @shashankvashishtha4454 หลายเดือนก่อน

    understand

  • @tejanshsachdeva487
    @tejanshsachdeva487 ปีที่แล้ว +1

    The typecasted leet-code code:
    #include
    #include
    #include
    class Solution {
    private:
    int maxEl(vector& piles) {
    int maxi = INT_MIN;
    for (int i = 0; i < piles.size(); i++) {
    maxi = max(maxi, piles[i]);
    }
    return maxi;
    }
    long long totEl(vector& piles, int mid) {
    long long totH = 0;
    int n = piles.size();
    for (int i = 0; i < n; i++) {
    totH += (piles[i] + mid - 1) / mid; // Updated calculation
    }
    return totH;
    }
    public:
    int minEatingSpeed(vector& piles, int h) {
    int low = 1;
    long long high = maxEl(piles);
    int ans = INT_MAX;
    while (low

  • @rushidesai2836
    @rushidesai2836 2 หลายเดือนก่อน

    public int findHours(int[] arr, int bananaCount) {
    int totalHrs = 0;
    for (int i = 0; i < arr.length; i++) {
    totalHrs += Math.ceil((double) arr[i] / bananaCount);
    }
    return totalHrs;
    }
    Cast to double, because ceil here needs floating-point number.

  • @debanjan10
    @debanjan10 ปีที่แล้ว

  • @arunm619
    @arunm619 26 วันที่ผ่านมา

    ceil can be replaced with
    x / a + ( (x % a) != 0 ? 1 : 0)

  • @vrandakansal5940
    @vrandakansal5940 ปีที่แล้ว

    Kb tk playlist complete hogi?

  • @lakshyabhati2262
    @lakshyabhati2262 5 หลายเดือนก่อน

    but if the array is sorted then we don't need to find max of array .. its just the last element of the array .. i guess

  • @nehathakur40
    @nehathakur40 ปีที่แล้ว

    how to tackle case of overflow in total hours calculated even if we use long long to calculate for total hours required it is giving wa?

    • @abhishek_dagar
      @abhishek_dagar 11 หลายเดือนก่อน

      use long long toatl hours instead of int

  • @sanjaysubramani4041
    @sanjaysubramani4041 26 วันที่ผ่านมา

    Finding max will add O(n) to the time complexity.won't it?

  • @durgaprasad-gn4dk
    @durgaprasad-gn4dk 10 หลายเดือนก่อน

    how can we come to know that the given question is bs on answers.

  • @srinathkumar9247
    @srinathkumar9247 5 หลายเดือนก่อน

    Lower boundry

  • @frostyfreezemovies
    @frostyfreezemovies ปีที่แล้ว +1

    I am using javascript😇

  • @darven7511
    @darven7511 6 หลายเดือนก่อน

    why do you return low, you didn't explained that part

  • @rollercoaster9719
    @rollercoaster9719 ปีที่แล้ว

    binary search on answer standard question

  • @abdussalam-4836
    @abdussalam-4836 10 วันที่ผ่านมา

    why i feel like i am watching these videos again

  • @channelcuriousity6467
    @channelcuriousity6467 5 หลายเดือนก่อน +1

    Test case 125 faiiled
    out of 126

  • @tarunprajapati7642
    @tarunprajapati7642 ปีที่แล้ว

    how to quickly understand that low is pointing to answer or high?

    • @abhishek_dagar
      @abhishek_dagar 11 หลายเดือนก่อน

      dry run by taking 1 example