BS-12. Koko Eating Bananas

แชร์
ฝัง
  • เผยแพร่เมื่อ 31 ม.ค. 2025

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

  • @abhaysinghrathore3192
    @abhaysinghrathore3192 ปีที่แล้ว +70

    give this man the best teacher award in the world

    • @30sunique78
      @30sunique78 11 หลายเดือนก่อน +8

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

  • @vm1662
    @vm1662 ปีที่แล้ว +109

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

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

    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 8 หลายเดือนก่อน

      same

  • @LokeshPandey-m1m
    @LokeshPandey-m1m ปีที่แล้ว +17

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

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

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

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

    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.

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

      Bro please post the solution of code

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

      @@Rider66778 ** 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 ปีที่แล้ว +2

      how far have you progressed in 3 weeks bro pls update

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

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

  • @pradyumnmulegaon385
    @pradyumnmulegaon385 ปีที่แล้ว +130

    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 ปีที่แล้ว +10

      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 ปีที่แล้ว +2

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

    • @ArunsinghParihar-j3j
      @ArunsinghParihar-j3j 11 หลายเดือนก่อน +3

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

    • @tusharkhatri2921
      @tusharkhatri2921 10 หลายเดือนก่อน +2

      Bro on LeetCode not working after 121 test cases.

    • @ranjeet_sohanpal
      @ranjeet_sohanpal 10 หลายเดือนก่อน +5

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

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

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

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

    Thank you, Striver. Loved the intuition on "binary search on answers" and on how to pick low or high as final answer.
    One quick suggestion:
    The python code provided in the solution is INCORRECT because ceil computation needs to be float.

  • @akashdutta1620
    @akashdutta1620 ปีที่แล้ว +10

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

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

    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
    @SuvradipDasPhotographyOfficial ปีที่แล้ว +3

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

  • @impalash_ag
    @impalash_ag 6 หลายเดือนก่อน +9

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

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

    I see Koko is on a high carb diet😀😅

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

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

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

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

  • @AnilKumar-mg8sj
    @AnilKumar-mg8sj 4 หลายเดือนก่อน

    Thanks!

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

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

  • @ErenYeager-dp4er
    @ErenYeager-dp4er 19 วันที่ผ่านมา +2

    Done on 12 Jan 2025 at 21:27
    Place : Study Room 2 , Hostel 5 , IIT Bombay

    • @mishra-h8l
      @mishra-h8l 4 วันที่ผ่านมา

      which year?

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

    STRIVERS TIP always find out the range ie low and high for applying the binary search bcz it could even work for larger ranges but will be efficient for the lowest high or the highest low.
    TIP 2 the pointers high and low always end up at opposite polarity of the condition ie for ex here POINTER low from being the speed in which eating in time isnt possible end up to possible minimum
    so without using the ANS directly use the POINTERS

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

    use long long to calculateTotalHours instead of int to avoid overflow

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

    I have no doubts after watching ur videos.

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

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

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

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

  • @sanjaybhorkar8068
    @sanjaybhorkar8068 20 วันที่ผ่านมา +1

    i have solved this question with the same approach and solution

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

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

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

    logic is same as finding first occurance THANK YOU

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

    Thanks a lot Striver!

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

    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 ปีที่แล้ว +16

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

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

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

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

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

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

      @@jayant-baid thanks 👍

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

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

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

    Understood
    Please do upload videos consistently 😊

  • @Wordlybee
    @Wordlybee 28 วันที่ผ่านมา

    For all those who are getting confused in time complexity
    Let the max element in the array be M
    1. TC wrt Brute force approach : N *M (haa pata hai, i am not using big O sign.. but smjh jaao tum log)
    Reason (Skip if already understood) : a) inner summation loop calc : N times
    b) Outer loop will go till max elmt in arr that is M times
    2. TC wrt optimal : N*log M
    Reason : Since instead of doing linear search in outer loop we used BS, so M was replaced by log M
    Note : N^2 >> N logN >> N >> logN
    CLEARING YOUR CONFUSION STARIGHTWAY : Would not be it O(N) instead of N logM ...Read note above...
    N logM is bigger than log N so will not consider it
    {Jo log aalas kar rahe hai itna bada para padhne me, kripya DSA kr rhe hai to yaha bhi mehnat kr lijiye}

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

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

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

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

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

      Can you explain the logic?

    • @ictfan-ly8gh
      @ictfan-ly8gh 5 วันที่ผ่านมา

      Mtlb kuch bhi 😂.

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

    Great explanation Striver

  • @AtulKumar-c4x7l
    @AtulKumar-c4x7l ปีที่แล้ว +1

    understood
    Thank you striver for such an amazing explanation..

  • @debanjanghosal618
    @debanjanghosal618 4 หลายเดือนก่อน +2

    I solved this problem but I cant get over 1 doubt? Why are we setting low to 1? Won't it be better if we set low as the minimum number of bananas among all the piles?

    • @ictfan-ly8gh
      @ictfan-ly8gh 29 วันที่ผ่านมา

      No .
      We will set low 1 only .
      We need k to be minimum.
      Consider any case --
      Take h=sum of all the elements of the array piles
      Then in such cases the minimum value of k will be 1.

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

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

  • @DikshaNarang-q9l
    @DikshaNarang-q9l ปีที่แล้ว +3

    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 ปีที่แล้ว +1

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

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

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

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

    Thank you Striver

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

    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

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

    You are just incredible ❤️🎉🎉

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

    Instead of taking low = 1, we can consider
    let low = Math.ceil(sum(piles) / h), high = Math.max(...piles);

    • @ictfan-ly8gh
      @ictfan-ly8gh 29 วันที่ผ่านมา

      We can do this .
      But idk why type casting of piles[i] using float gives error .
      Can u tell me

    • @user-rdr1712
      @user-rdr1712 29 วันที่ผ่านมา

      @ What error?

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

    Understood, Thanks striver for this amazing video.

  • @Pw_Unfiltered
    @Pw_Unfiltered 19 วันที่ผ่านมา

    two doubts for optimal approach :(
    1. when we are finding total time then it will iterate for each element thus making complexity as Nlog(max element) which i guess is not better than log(Max element)
    2. you did it for sorted array, but if it is unsorted then how will we be sure to reject one half of the array

    • @ictfan-ly8gh
      @ictfan-ly8gh 5 วันที่ผ่านมา

      Which approach is having t.c O(log max).
      Secondly which sorted array you are taking about. Piles ?
      That will work whether the piles is sorted or not

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

    Really amazing explanation 👏

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

    Understood Man You damn it!!!

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

    Got asked this question today!

  • @ayushgoel01
    @ayushgoel01 ปีที่แล้ว +4

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

  • @srinivasareddychalla-i2o
    @srinivasareddychalla-i2o ปีที่แล้ว +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.

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

    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

  • @AnmolGupta-oj4lm
    @AnmolGupta-oj4lm ปีที่แล้ว

    Understood Very Well!

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

    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 ??.

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

    Dada tumi best❤❤❤

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

    to clear all the test cases . class
    Solution {
    public:
    int findmaxelement(vector& piles){
    int maximum = INT_MIN;
    int n = piles.size();
    for (int i=0; i

    • @ictfan-ly8gh
      @ictfan-ly8gh 29 วันที่ผ่านมา

      Can't we use float for type cast

    • @ictfan-ly8gh
      @ictfan-ly8gh 5 วันที่ผ่านมา

      When using float 123 testcase is not passing .
      But after I did double all testcase passed . What's the logic now

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

  • @ShivamMaurya-fq6ws
    @ShivamMaurya-fq6ws ปีที่แล้ว

    @striver thanks for explanation

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

    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.

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

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

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

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

  • @VishalGupta-xw2rp
    @VishalGupta-xw2rp ปีที่แล้ว

    16:50 Low opposite polarity

  • @Shikaslad-s5f
    @Shikaslad-s5f 8 หลายเดือนก่อน

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

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

    very helpful explaination

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

    thank youu striver

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

    Really Amazing👏👏👏

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

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

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

    Best explanation 🔥

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

    A typical De-Shaw intern OA question

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

    was waiting for this

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

    Best explaination😍

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

    Understood✅🔥🔥

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

    nice explaination

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

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

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

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

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

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

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

    i got a q same like this in uber where hours were same but here it is a car and we need to find min kwperh so that the cars can get charged
    . but right now i am in my third year and gave uber in the beginning of the year and i didnt have any idea abt this. thank go i am on a strict plan of action now hope i will be able to solve q next time in OA

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

    Understood, thank you.

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

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

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

    Understood!

  • @ZaminRashid-vc5zb
    @ZaminRashid-vc5zb 4 หลายเดือนก่อน

    bro the array is already sorted so we know that the maximum element will be at the end... why are you writing function to calculate maximum?

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

      the array may or may not be sorted so you need a helper function to find the max

  • @sateeshkumar1634
    @sateeshkumar1634 11 หลายเดือนก่อน +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....

  • @abdussalam-4836
    @abdussalam-4836 6 หลายเดือนก่อน +1

    why i feel like i am watching these videos again

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

    Understood😁

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

    Nice explanation ❤

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

    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.

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

    understood 😇

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

    Solved this without watching the video myself

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

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

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

    Good explanation understood

  • @onetapgaming123-v2x
    @onetapgaming123-v2x 7 วันที่ผ่านมา +1

    24/01/25 Uber She++ question 🤩

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

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

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

    Following this series. Really helpfull 💪💪

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

    • @ictfan-ly8gh
      @ictfan-ly8gh 29 วันที่ผ่านมา

      But bro time complexity of ceil function is O(1)
      ( Sqrt function has time complexity generally O(1) .
      )

  • @HyperV3
    @HyperV3 11 วันที่ผ่านมา

    Max=n-1 ?cause it is sorted?

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

    Was Waiting for the same

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

    Thank You

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

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

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

    Understood ❤

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

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

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

    The TC will be O(n) or what?

  • @Aryan-j7i2b
    @Aryan-j7i2b 2 หลายเดือนก่อน

    understood!!!

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

    Kb tk playlist complete hogi?

  • @durgaprasad-gn4dk
    @durgaprasad-gn4dk ปีที่แล้ว

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