Koko Eating Bananas Leetcode 875. Binary Search | Full C++ Code in Comments | Brute force to Optimal

Sdílet
Vložit
  • čas přidán 27. 07. 2024
  • Timestamps:
    0:00 Reading out question
    1:51 Brute force example
    6:42 Intuition to optimize
    8:55 Time Complexity
    9:50 Optimized example
    13:24 Code
    Koko loves to eat bananas. There are n piles of bananas, the ith pile has piles[i] bananas. The guards have gone and will come back in h hours.
    Koko can decide her bananas-per-hour eating speed of k. Each hour, she chooses some pile of bananas and eats k bananas from that pile. If the pile has less than k bananas, she eats all of them instead and will not eat any more bananas during this hour.
    Koko likes to eat slowly but still wants to finish eating all the bananas before the guards return.
    Return the minimum integer k such that she can eat all the bananas within h hours.
    Example 1:
    Input: piles = [3,6,7,11], h = 8
    Output: 4
    Example 2:
    Input: piles = [30,11,23,4,20], h = 5
    Output: 30
    Example 3:
    Input: piles = [30,11,23,4,20], h = 6
    Output: 23
    Check out our other playlists:
    Dynamic Programming:
    • Dynamic Programming
    Trees:
    • Trees
    Heaps and Maps:
    • Heaps and Maps
    Arrays and Maths:
    • Arrays and Maths
    Bit Manipulation:
    • Bit Manipulation
    Greedy Algorithms:
    • Greedy Algorithms
    Sorting and Searching:
    • Sorting and Searching
    Strings:
    • Strings
    Linked Lists:
    • Linked Lists
    Stack and Queues:
    • Stacks and Queues
    Two Pointers:
    • Two pointers
    Graphs, BFS, DFS:
    • Graphs, DFS, BFS
    Backtracking:
    • Backtracking
    Non- DSA playlists:
    Probability:
    • Probability
    SQL-Basic Join functions:
    • SQL - Basic JOIN Funct...
    SQL-Basic Aggregate functions:
    • SQL-Basic Aggregate Fu...
  • Věda a technologie

Komentáře • 26

  • @probabilitycodingisfunis1
    @probabilitycodingisfunis1  Před 2 lety +14

    Code in C++ -> Time Complexity -> O(log(m)*n) where m is max element in array
    int minEatingSpeed(vector& piles, int h) {

    int low = 1;
    int high = *max_element(piles.begin(),piles.end());

    int ans = 0;
    while(low

  • @abhivarma1756
    @abhivarma1756 Před rokem +2

    thanks for the solution! You deserve way more subscribers for your content.

  • @satyamgupta6030
    @satyamgupta6030 Před rokem

    tysm alisha for such a wonderful explaination.

  • @gowrisankar98jammu49
    @gowrisankar98jammu49 Před rokem +1

    awesome explanations mam
    🤩🤩

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

    much love to your work Alisha, you made my day ❤, Thanks a lot!!

  • @AkshayKumar-oi2cu
    @AkshayKumar-oi2cu Před rokem

    you are great source strength and motivation for us ...

  • @nkthehustler
    @nkthehustler Před rokem

    thanks for the explaination

  • @gaurangshroff
    @gaurangshroff Před rokem +1

    best explanation of this problem so far

  • @AmanSharma-vb5jl
    @AmanSharma-vb5jl Před 2 lety

    U are awesome explainer

  • @user-zn3be9ik1x
    @user-zn3be9ik1x Před 11 měsíci

    Best explanation

  • @hemanthn436
    @hemanthn436 Před rokem

    simply superb

  • @thiyageshkanna
    @thiyageshkanna Před 2 lety

    Explanation is damn good

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

    class Solution {
    public:
    int minEatingSpeed(vector& piles, int h) {
    int low = 1;
    int high =10000000000;

    int ans = 0;
    while (low

  • @none2868
    @none2868 Před rokem +2

    Solution didnt work

  • @saraswatirathore3933
    @saraswatirathore3933 Před 2 lety

    Thankful for your wonderful efforts which help me to understand this problem

  • @alt-f4gaming222
    @alt-f4gaming222 Před rokem

    gajab yaar , ek number ../// thank u so mmuchh

  • @AdityaSharma-nr7qn
    @AdityaSharma-nr7qn Před rokem

    amazing explanation and don't stop uploading

  • @ayanmalik1430
    @ayanmalik1430 Před 2 lety +2

    Great explanations !! _/\_

  • @yashsinha5385
    @yashsinha5385 Před rokem +1

    class Solution {
    public:
    int minEatingSpeed(vector& piles, int h) {
    int low = 1;
    int high = *max_element(piles.begin(),piles.end());
    int ans = 0;
    while(low

    • @divyanshusahu6111
      @divyanshusahu6111 Před rokem +2

      You are returning long long int in integer return type function

    • @stuartYoung559
      @stuartYoung559 Před rokem +2

      exactly , in leetcote as well as in gfg , answer is not coming out right by this solution

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

      int k=0;
      int left=1, right= 10000000000;

      while(left

    • @suhanigupta2861
      @suhanigupta2861 Před 8 měsíci

      @@ashishkulkarni2350 This works! Thank you so much!
      However,
      Can you please provide the explanation of the line:
      count+= (piles[i]+k-1)/k;

  • @eddiej204
    @eddiej204 Před rokem

    Great explanation, thanks a lot miss. 🙏🫡