githubEdit

10. Koko Eating Bananas

βœ… GFG solution to the Koko Eating Bananas problem: find minimum eating speed using binary search optimization technique. πŸš€

The problem can be found at the following link: πŸ”— Question Linkarrow-up-right

🧩 Problem Description

Koko is given an array arr[], where each element represents a pile of bananas. She has exactly k hours to eat all the bananas.

Each hour, Koko can choose one pile and eat up to s bananas from it:

  • If the pile has at least s bananas, she eats exactly s bananas.

  • If the pile has fewer than s bananas, she eats the entire pile in that hour.

  • Koko can only eat from one pile per hour.

Your task is to find the minimum value of s (bananas per hour) such that Koko can finish all the piles within k hours.

πŸ“˜ Examples

Example 1

Input: arr[] = [5, 10, 3], k = 4
Output: 5
Explanation: If Koko eats at the rate of 5 bananas per hour:
- First pile of 5 bananas will be finished in 1 hour.
- Second pile of 10 bananas will be finished in 2 hours.
- Third pile of 3 bananas will be finished in 1 hour.
Therefore, Koko can finish all piles in 1 + 2 + 1 = 4 hours.

Example 2

πŸ”’ Constraints

  • $1 \le \text{arr.size()} \le k \le 10^6$

  • $1 \le \text{arr}[i] \le 10^6$

βœ… My Approach

The optimal approach uses Binary Search on the answer space to efficiently find the minimum eating speed:

Binary Search on Answer

  1. Define Search Space:

    • Minimum possible speed: 1 banana per hour (slowest)

    • Maximum possible speed: max(arr) bananas per hour (fastest - eating largest pile in 1 hour)

  2. Binary Search Logic:

    • For each candidate speed mid, calculate total hours needed to eat all bananas

    • Hours for each pile = ⌈pile_size / midβŒ‰ = (pile_size + mid - 1) / mid

    • Sum up hours for all piles

  3. Decision Making:

    • If total hours ≀ k: This speed works, but try to minimize further (search left half)

    • If total hours > k: Speed too slow, need faster speed (search right half)

  4. Update Answer:

    • Whenever we find a valid speed, store it as potential answer

    • Continue searching for minimum valid speed

  5. Termination:

    • When search space exhausted, return the minimum valid speed found

πŸ“ Time and Auxiliary Space Complexity

  • Expected Time Complexity: O(n * log m), where n is the size of the array and m is the maximum element in the array. Binary search runs in O(log m) iterations, and for each iteration, we traverse the entire array in O(n) to calculate total hours needed.

  • Expected Auxiliary Space Complexity: O(1), as we only use a constant amount of additional space for variables like pointers, mid value, and hour counter.

πŸ§‘β€πŸ’» Code (C++)

chevron-right⚑ View Alternative Approaches with Code and Analysishashtag

πŸ“Š 2️⃣ Optimized Binary Search with Range

πŸ’‘ Algorithm Steps:

  1. Calculate minimum possible speed using total bananas and hours: ⌈total_bananas / kβŒ‰

  2. Set upper bound as maximum pile to narrow search space

  3. Use binary search with optimized mid calculation

  4. Track answer and minimize search range efficiently without storing intermediate answer

πŸ“ Complexity Analysis:

  • Time: ⏱️ O(n * log m) - Binary search with optimized bounds

  • Auxiliary Space: πŸ’Ύ O(1) - Constant space for variables

βœ… Why This Approach?

  • Tighter lower bound reduces search iterations

  • Avoids unnecessary checks in lower range

  • Better average case performance

  • Cleaner binary search template (no separate ans variable)

πŸ†š πŸ” Comparison of Approaches

πŸš€ Approach

⏱️ Time Complexity

πŸ’Ύ Space Complexity

βœ… Pros

⚠️ Cons

πŸ” Standard Binary Search

🟒 O(n * log m)

🟒 O(1)

πŸš€ Clean & straightforward

πŸ”§ May check unnecessary lower values

⚑ Optimized Range

🟒 O(n * log m)

🟒 O(1)

⭐ Better lower bound, fewer iterations

πŸ”§ Extra calculation for sum needed

πŸ† Best Choice Recommendation

🎯 Scenario

πŸŽ–οΈ Recommended Approach

πŸ”₯ Performance Rating

πŸ… Simplicity and readability

πŸ₯‡ Standard Binary Search

β˜…β˜…β˜…β˜…β˜…

🎯 Large datasets with tight time limits

πŸ₯ˆ Optimized Range

β˜…β˜…β˜…β˜…β˜…

⚑ Competitive programming

πŸ₯ˆ Optimized Range

β˜…β˜…β˜…β˜…β˜…

β˜• Code (Java)

🐍 Code (Python)

🧠 Contribution and Support

For discussions, questions, or doubts related to this solution, feel free to connect on LinkedIn: πŸ“¬ Any Questions?arrow-up-right. Let's make this learning journey more collaborative!

⭐ If you find this helpful, please give this repository a star! ⭐


πŸ“Visitor Count

Visitor counter

Last updated