BS-12. Koko Eating Bananas

Поділитися
Вставка
  • Опубліковано 27 вер 2024
  • Problem Link: bit.ly/3CmCKVI
    Notes/C++/Java/Python codes: takeuforward.o...
    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/take...
    0:00 Introduction of Course

КОМЕНТАРІ • 272

  • @--Blood--Prince--
    @--Blood--Prince-- Рік тому +190

    I see Koko is on a high carb diet😀😅

  • @vm1662
    @vm1662 Рік тому +70

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

  • @abhaysinghrathore3192
    @abhaysinghrathore3192 11 місяців тому +12

    give this man the best teacher award in the world

    • @30sunique78
      @30sunique78 6 місяців тому +2

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

  • @pradyumnmulegaon385
    @pradyumnmulegaon385 9 місяців тому +86

    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 9 місяців тому +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 9 місяців тому +1

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

    • @ArunsinghParihar-j3j
      @ArunsinghParihar-j3j 7 місяців тому +3

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

    • @tusharkhatri2921
      @tusharkhatri2921 6 місяців тому +1

      Bro on LeetCode not working after 121 test cases.

    • @ranjeet_sohanpal
      @ranjeet_sohanpal 5 місяців тому +3

      @@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

  • @sauravchandra10
    @sauravchandra10 Рік тому +33

    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!

  • @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❤❤

  • @visheshagrawal8676
    @visheshagrawal8676 Рік тому +8

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

  • @SuvradipDasPhotographyOfficial

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

  • @Amanali-rl9hw
    @Amanali-rl9hw Рік тому +1

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

  • @CrazyHunk14
    @CrazyHunk14 7 місяців тому +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

  • @impalash_ag
    @impalash_ag 2 місяці тому +1

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

  • @visheshagrawal8676
    @visheshagrawal8676 Рік тому +3

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

  • @knowthrvdo
    @knowthrvdo 7 місяців тому

    logic is same as finding first occurance THANK YOU

  • @Manishgupta200
    @Manishgupta200 Рік тому +2

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

  • @cinime
    @cinime Рік тому +1

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

  • @AtulKumar-c4x7l
    @AtulKumar-c4x7l Рік тому +1

    understood
    Thank you striver for such an amazing explanation..

  • @arvindokram2906
    @arvindokram2906 Місяць тому

    Got asked this question today!

  • @hareshnayak7302
    @hareshnayak7302 6 місяців тому

    Understood, Thanks striver for this amazing video.

  • @DikshaNarang-q9l
    @DikshaNarang-q9l Рік тому +2

    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 8 місяців тому

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

  • @joeljacob4685
    @joeljacob4685 Рік тому

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

  • @rushidesai2836
    @rushidesai2836 4 місяці тому

    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.

  • @Jus-chill
    @Jus-chill Рік тому +1

    Understood
    Please do upload videos consistently 😊

  • @manavsingh5919
    @manavsingh5919 Рік тому

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

  • @aniketgupta8064
    @aniketgupta8064 Рік тому +19

    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 11 місяців тому +2

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

  • @abdussalam-4836
    @abdussalam-4836 2 місяці тому +1

    why i feel like i am watching these videos again

  • @tanujaSangwan
    @tanujaSangwan 15 днів тому

    Solved this without watching the video myself

  • @Shikaslad-s5f
    @Shikaslad-s5f 4 місяці тому

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

  • @RaviRavi-kt9gt
    @RaviRavi-kt9gt Рік тому

    You are just incredible ❤️🎉🎉

  • @AnilKumar-mg8sj
    @AnilKumar-mg8sj 10 днів тому

    Thanks!

  • @sateeshkumar1634
    @sateeshkumar1634 7 місяців тому +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....

  • @abhaythakur2597
    @abhaythakur2597 Рік тому

    very helpful explaination

  • @culeforever5408
    @culeforever5408 11 місяців тому +1

    understood 😇

  • @ShivamMaurya-fq6ws
    @ShivamMaurya-fq6ws 11 місяців тому

    @striver thanks for explanation

  • @AnmolGupta-oj4lm
    @AnmolGupta-oj4lm Рік тому

    Understood Very Well!

  • @DeadPoolx1712
    @DeadPoolx1712 Місяць тому

    UNDERSTOOD;

  • @Thescienceworld652
    @Thescienceworld652 Рік тому +1

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

  • @harshitjaiswal9439
    @harshitjaiswal9439 Рік тому +1

    Understood!

  • @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

  • @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?

  • @NazeerBashaShaik
    @NazeerBashaShaik 5 місяців тому

    Understood, thank you.

  • @aaranyaksantra9933
    @aaranyaksantra9933 3 місяці тому

    nice explaination

  • @akashgite905
    @akashgite905 2 місяці тому

    koko needs to stop eating those bananas

  • @codeman3828
    @codeman3828 Місяць тому

    Understood

  • @dronnema203
    @dronnema203 25 днів тому

    understood

  • @umangkumar2005
    @umangkumar2005 7 місяців тому

    little explanation solve the whole question #include
    using namespace std;
    bool fun(vector & v,int h ,int &ans, int mid){
    int sum=0;
    for(int i=0;ih){
    return true;
    }
    }
    ans=mid;
    return false;
    }
    int minimumRateToEatBananas(vector v, int h) {
    int ma=INT_MIN;
    for(int i=0;i

  • @suneethkumarpolaki3441
    @suneethkumarpolaki3441 19 днів тому

    is max function doesn't take O(N) time

  • @yashendrabadal4776
    @yashendrabadal4776 3 місяці тому +1

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

  • @dayashankarlakhotia4943
    @dayashankarlakhotia4943 Рік тому

    Good explanation understood

  • @srinivasareddychalla-i2o
    @srinivasareddychalla-i2o 10 місяців тому +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.

  • @sanjaysubramani4041
    @sanjaysubramani4041 2 місяці тому

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

  • @neelnsoni1313
    @neelnsoni1313 Рік тому

    Thank You

  • @AdityaYadav-qf9qc
    @AdityaYadav-qf9qc Рік тому

    Best explaination😍

  • @televisionstardom578
    @televisionstardom578 5 днів тому

    Koko needs to do fast for atleast one year .

  • @Learnprogramming-q7f
    @Learnprogramming-q7f 7 місяців тому

    Thank you bhaiya

  • @arunm619
    @arunm619 2 місяці тому

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

  • @ayushgoel01
    @ayushgoel01 Рік тому +4

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

  • @debhagaming
    @debhagaming Рік тому

    Nice explanation ❤

  • @ziggywiggys
    @ziggywiggys 3 місяці тому

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

  • @frostyfreezemovies
    @frostyfreezemovies Рік тому +1

    I am using javascript😇

  • @channelcuriousity6467
    @channelcuriousity6467 7 місяців тому +1

    Test case 125 faiiled
    out of 126

  • @YourCodeVerse
    @YourCodeVerse 9 місяців тому

    Understood✅🔥🔥

  • @Yahsaswi
    @Yahsaswi Рік тому

    was waiting for this

  • @srushtibhagchandani7255
    @srushtibhagchandani7255 3 місяці тому

    Understood!!

  • @Donquixote-Rosinante
    @Donquixote-Rosinante 9 місяців тому

    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?

  • @darven7511
    @darven7511 8 місяців тому

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

  • @lakshyabhati2262
    @lakshyabhati2262 7 місяців тому

    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

  • @harshilpatel3205
    @harshilpatel3205 7 місяців тому

    Understood sir 🫡🤍

  • @sumitdon2004
    @sumitdon2004 2 місяці тому

    striver is best

  • @abhishek_dagar
    @abhishek_dagar Рік тому +1

    Leetcode solution all test cases passed :
    class Solution {
    public:
    int findmaximum(vector &piles){
    int n = piles.size();
    int maxi = INT_MIN;
    for(int i =0; i

  • @mahijain5053
    @mahijain5053 Місяць тому

    so if an array is not sorted, can this logic still work, please tell

    • @RavikantMunda
      @RavikantMunda Місяць тому

      Yes it will work fine with the same code because our initial search space is [1, max(piles)] not the piles array

  • @shashankvashishtha4454
    @shashankvashishtha4454 3 місяці тому

    understand

  • @srinathv1412
    @srinathv1412 7 місяців тому

    Understood!!!!!

  • @AbhishekBhattacharjee-j2m
    @AbhishekBhattacharjee-j2m Рік тому

    UNDERSTOOD

  • @debanjan10
    @debanjan10 Рік тому

  • @amaankhan9845
    @amaankhan9845 Рік тому

    Following this series. Really helpfull 💪💪

  • @A_Myth963
    @A_Myth963 6 місяців тому

    For people doing on leetcode and getting error after testcase 120 ...just use long long hrtaken and also return long long..
    Here's the code if someone needs it
    class Solution {
    public:
    long long hrtaken(vector pi, int h) {
    long long hr = 0;
    for (auto i : pi) {
    hr += ceil((double)i/(double)h);
    }
    return hr;
    }
    int minEatingSpeed(vector& piles, int h) {
    int maxt = -1;
    int m = -1;
    for (auto i : piles)
    m = max(m, i);
    int low = 1, high = m;
    while (low

  • @durgaprasad-gn4dk
    @durgaprasad-gn4dk Рік тому

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

  • @udaykiranmogalpau8292
    @udaykiranmogalpau8292 Рік тому

    understood

  • @amitranjeetjha1240
    @amitranjeetjha1240 Рік тому

    Understood

  • @RHuL706
    @RHuL706 Рік тому +1

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

    • @codesetter2497
      @codesetter2497 Місяць тому

      you are 5 year of experience and asking questions like a freshers
      It means from the year wise you are 5 years exp but experience/Knowledge wise you are fresher have no idea

  • @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?

  • @pentapreethika4901
    @pentapreethika4901 9 місяців тому

    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.

  • @deeptidip9864
    @deeptidip9864 Рік тому

    Understood:)

  • @googleit2490
    @googleit2490 Рік тому

    Understood :)

  • @pihus3498
    @pihus3498 Рік тому

    understood :)

  • @vatsalyasharma5585
    @vatsalyasharma5585 Рік тому

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

  • @tarunprajapati7642
    @tarunprajapati7642 Рік тому

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

  • @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?

  • @priyankasetiya1358
    @priyankasetiya1358 3 місяці тому

    class Solution {
    int ans=-1;
    private int binarySearch(int[] piles,int h,int low,int high){
    if(low>high){
    return ans;
    }
    int mid = low+(high-low)/2;
    if(findRequiredHours(piles,mid)

  • @akashkumaryadav1827
    @akashkumaryadav1827 Місяць тому

    🔥

  • @vrandakansal5940
    @vrandakansal5940 Рік тому

    Kb tk playlist complete hogi?

  • @23cash86
    @23cash86 Рік тому

    Thanks++

  • @kabhinavsarma9224
    @kabhinavsarma9224 2 місяці тому

    was asked in de shaw

  • @taun8768
    @taun8768 5 місяців тому

    Its failing in last test case

    • @iamprateek3220
      @iamprateek3220 5 місяців тому

      use long long for totalhour data type every where

  • @abhishekbindra9043
    @abhishekbindra9043 3 місяці тому

    sir even after submitting the exactly same code as yours...its getting wrong on 10th test case

    • @abhishekshinde5375
      @abhishekshinde5375 2 місяці тому +1

      add this condition to prevent overflow while calculating totalHours required to eat for the current rate
      if(timeReq > h){
      break;
      }
      or use long long datatype every where

  • @rohankadam7376
    @rohankadam7376 Рік тому

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

    • @abhishek_dagar
      @abhishek_dagar Рік тому +1

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

  • @surajs5093
    @surajs5093 Рік тому

    spent whole day in this problem still could not do it

  • @khushil9988
    @khushil9988 7 місяців тому

    but yh to sorted nhi to binary kese laga

    • @saswatrath4646
      @saswatrath4646 5 місяців тому

      search space is sorted bro, our answer doesn't lie isnide piles array, it lies inside the range [1, maxelement] which is sorted.

    • @khushil9988
      @khushil9988 5 місяців тому

      damn now I get it thanks brother

  • @AshishSahu-ef9rl
    @AshishSahu-ef9rl 14 днів тому

    koko k tho maje hi maje

  • @codingp110
    @codingp110 4 місяці тому

    Uunderstood