BS-12. Koko Eating Bananas

Sdílet
Vložit
  • čas přidán 20. 06. 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

Komentáře • 237

  • @--Blood--Prince--
    @--Blood--Prince-- Před rokem +144

    I see Koko is on a high carb diet😀😅

  • @vm1662
    @vm1662 Před 10 měsíci +57

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

  • @pradyumnmulegaon385
    @pradyumnmulegaon385 Před 7 měsíci +64

    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 Před 7 měsíci +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 Před 7 měsíci +1

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

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

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

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

      Bro on LeetCode not working after 121 test cases.

    • @ranjeet_sohanpal
      @ranjeet_sohanpal Před 3 měsíci +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 Před rokem +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❤❤

  • @sauravchandra10
    @sauravchandra10 Před rokem +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!

  • @visheshagrawal8676
    @visheshagrawal8676 Před rokem +7

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

  • @CrazyHunk14
    @CrazyHunk14 Před 5 měsíci +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

  • @SuvradipDasPhotographyOfficial

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

  • @user-eh9wd8hp4f
    @user-eh9wd8hp4f Před rokem +7

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

  • @cinime
    @cinime Před rokem +1

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

  • @thenikhildaiya.
    @thenikhildaiya. Před rokem +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 Před rokem +1

      Bro please post the solution of code

    • @thenikhildaiya.
      @thenikhildaiya. Před rokem +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 Před rokem +1

      how far have you progressed in 3 weeks bro pls update

    • @thenikhildaiya.
      @thenikhildaiya. Před rokem

      @@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 Před rokem

      @@thenikhildaiya. Which topic is aggressive cows in?

  • @abhaysinghrathore3192
    @abhaysinghrathore3192 Před 9 měsíci +4

    give this man the best teacher award in the world

    • @30sunique78
      @30sunique78 Před 4 měsíci

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

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

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

  • @user-ti3bd8mp1w
    @user-ti3bd8mp1w Před rokem +1

    understood
    Thank you striver for such an amazing explanation..

  • @Dipanshutripathi2407
    @Dipanshutripathi2407 Před 10 měsíci

    I have no doubts after watching ur videos.

  • @varun1017
    @varun1017 Před 5 měsíci +4

    use long long to calculateTotalHours instead of int to avoid overflow

  • @RaviRavi-kt9gt
    @RaviRavi-kt9gt Před rokem

    You are just incredible ❤️🎉🎉

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

    logic is same as finding first occurance THANK YOU

  • @akashdutta1620
    @akashdutta1620 Před 7 měsíci +4

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

  • @Surya1prakash
    @Surya1prakash Před rokem +1

    Really amazing explanation 👏

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

    Understood, Thanks striver for this amazing video.

  • @joeljacob4685
    @joeljacob4685 Před 10 měsíci

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

  • @Amanali-rl9hw
    @Amanali-rl9hw Před rokem +1

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

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

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

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

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

  • @AnmolGupta-oj4lm
    @AnmolGupta-oj4lm Před 11 měsíci

    Understood Very Well!

  • @user-ny6tz2rb1r
    @user-ny6tz2rb1r Před 8 měsíci +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.

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

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

  • @AyushSharma-sd1ny
    @AyushSharma-sd1ny Před rokem

    Great explanation Striver

  • @ShivamMaurya-fq6ws
    @ShivamMaurya-fq6ws Před 9 měsíci

    @striver thanks for explanation

  • @Jus-chill
    @Jus-chill Před rokem +1

    Understood
    Please do upload videos consistently 😊

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

    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

  • @abhaythakur2597
    @abhaythakur2597 Před rokem

    very helpful explaination

  • @tanishkarawat5266
    @tanishkarawat5266 Před rokem +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?

  • @ayushjaiswal7298
    @ayushjaiswal7298 Před rokem

    Best explanation 🔥

  • @Yahsaswi
    @Yahsaswi Před rokem

    was waiting for this

  • @kiranmoura2974
    @kiranmoura2974 Před rokem

    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?

  • @AdityaYadav-qf9qc
    @AdityaYadav-qf9qc Před rokem

    Best explaination😍

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

    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.

  • @engineerssolutionpoint95

    Nice explanation ❤

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

    Understood, thank you.

  • @impalash_ag
    @impalash_ag Před 10 dny

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

  • @akshaykeswani3601
    @akshaykeswani3601 Před rokem

    Really Amazing👏👏👏

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

    Thank You

  • @visheshagrawal8676
    @visheshagrawal8676 Před rokem +2

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

  • @dayashankarlakhotia4943

    Good explanation understood

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

    nice explaination

  • @hitmanop4078
    @hitmanop4078 Před měsícem +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 ??.

  • @user-cb8ei9xp2b
    @user-cb8ei9xp2b Před 10 měsíci

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

  • @chethanprabhu4475
    @chethanprabhu4475 Před 14 dny

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

  • @Donquixote-Rosinante
    @Donquixote-Rosinante Před 7 měsíci

    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?

  • @harshitjaiswal9439
    @harshitjaiswal9439 Před rokem +1

    Understood!

  • @user-eb4ox5zv5m
    @user-eb4ox5zv5m Před 2 měsíci

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

  • @ravisingla9032
    @ravisingla9032 Před rokem

    Was Waiting for the same

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

    Thank you bhaiya

  • @pritivarse2129
    @pritivarse2129 Před rokem +1

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

  • @sateeshkumar1634
    @sateeshkumar1634 Před 5 měsíci +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....

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

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

  • @aniketgupta8064
    @aniketgupta8064 Před rokem +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 Před rokem +13

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

    • @aniketgupta8064
      @aniketgupta8064 Před rokem +3

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

    • @jayant-baid
      @jayant-baid Před rokem +8

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

    • @aniketgupta8064
      @aniketgupta8064 Před rokem +2

      @@jayant-baid thanks 👍

    • @MYMIND252
      @MYMIND252 Před 9 měsíci +2

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

  • @jhanavikumari274
    @jhanavikumari274 Před dnem

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

  • @arunm619
    @arunm619 Před 27 dny

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

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

    Understood ❤

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

    understood 😇

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

    Understood✅🔥🔥

  • @Manishgupta200
    @Manishgupta200 Před rokem +2

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

  • @aritramallik8173
    @aritramallik8173 Před rokem

    Dada tumi best❤❤❤

  • @nehathakur40
    @nehathakur40 Před rokem

    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 Před 11 měsíci

      use long long toatl hours instead of int

  • @vatsalyasharma5585
    @vatsalyasharma5585 Před rokem

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

  • @RHuL706
    @RHuL706 Před rokem +1

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

  • @sanjaysubramani4041
    @sanjaysubramani4041 Před 26 dny

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

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

    Understood!!

  • @durgaprasad-gn4dk
    @durgaprasad-gn4dk Před 10 měsíci

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

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

    understood

  • @Anony.musk01
    @Anony.musk01 Před rokem

    understoood!!!

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

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

  • @vrandakansal5940
    @vrandakansal5940 Před rokem

    Kb tk playlist complete hogi?

  • @ankithac5520
    @ankithac5520 Před rokem

    Understood

  • @rohankadam7376
    @rohankadam7376 Před rokem

    Can anyone explain why we have use double while using ceil function?

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

      #include
      using namespace std;
      int main() {
      int ans = ceil(4/(double)3);
      cout

  • @tarunprajapati7642
    @tarunprajapati7642 Před rokem

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

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

    great

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

    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

  • @amaankhan9845
    @amaankhan9845 Před rokem

    Following this series. Really helpfull 💪💪

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

    understand

  • @VishalGupta-xw2rp
    @VishalGupta-xw2rp Před 10 měsíci

    16:50 Low opposite polarity

  • @user-nk1mb5fy7j
    @user-nk1mb5fy7j Před rokem

    UNDERSTOOD

  • @noomanraza7884
    @noomanraza7884 Před 10 měsíci

    why low started from 1

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

    Understood!!!!!

  • @girikgarg8
    @girikgarg8 Před 10 měsíci

    Done

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

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

  • @deeptidip9864
    @deeptidip9864 Před rokem

    Understood:)

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

    Understood sir 🫡🤍

  • @23cash86
    @23cash86 Před rokem

    Thanks++

  • @pihus3498
    @pihus3498 Před rokem

    understood :)

  • @debanjan10
    @debanjan10 Před rokem

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

    Lower boundry

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

    Why mid can't be our ans , why low

  • @ayushgoel01
    @ayushgoel01 Před 11 měsíci +4

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

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

    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.

  • @rollercoaster9719
    @rollercoaster9719 Před rokem

    binary search on answer standard question

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

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

  • @user-xb1st2pm6i
    @user-xb1st2pm6i Před rokem

    What to do if the totalH value exceeds int limits ?

    • @vipinshukla5546
      @vipinshukla5546 Před rokem +1

      use long long instead of using int

    • @lucario404
      @lucario404 Před rokem

      Actually I was looking for this.....when will the totalH value exceed int limit, can you explain me?

    • @111rhishishranjan2
      @111rhishishranjan2 Před rokem

      @@lucario404 #include
      #include
      #include
      class Solution {
      public:
      int minEatingSpeed(vector& piles, int h) {
      int s = 1;
      sort(piles.begin(), piles.end());
      int ans = 0;
      int n = piles.size() - 1;
      int e = piles[n];

      while (s