Koko Eating Bananas Solution for Accenture

Company: Accenture · Difficulty: Medium · Topics: Array, Binary Search, Greedy

Problem Statement

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 from any more piles during this hour.

Koko likes to eat slowly but still wants to finish eating all the bananas before the guards come back.

Return the minimum integer k such that she can eat all the bananas within h hours.

Key Understanding:

Examples

Example 1:

Input: piles = [3,6,7,11], h = 8

Output: 4

Explanation:

Example 2:

Input: piles = [30,11,23,4,20], h = 5

Output: 30

Explanation:

Example 3:

Input: piles = [312884470], h = 312884469

Output: 2

Explanation:

Intuition

The key insight is to use binary search to find the minimum speed k. We can check if a given speed k is feasible by calculating how many hours it would take to eat all bananas.

Binary Search Approach:

Why Binary Search Works:

Approach

Approach 1: Binary Search (Optimal)

Algorithm:

1. Set left = 1, right = max(piles)

2. While left < right:

3. Return left

Hours Calculation:

Approach 2: Linear Search

Algorithm:

1. Try each speed from 1 to max(piles)

2. For each speed, calculate total hours needed

3. Return the first speed that works

Approach 3: Mathematical Approach

Algorithm:

1. Calculate total bananas = sum(piles)

2. Minimum speed = ceil(total_bananas / h)

3. Verify this speed works, if not, increment

Dry Run

Let's trace through Example 1: piles = [3,6,7,11], h = 8

Binary Search Approach:

Key Insights

1. Binary Search Range: Speed ranges from 1 to max(piles)

2. Monotonic Property: If speed k works, any speed > k also works

3. Hours Calculation: Use ceiling division for each pile

4. Optimal Solution: Binary search finds the minimum feasible speed

5. Edge Cases: Handle cases where no solution exists

Edge Cases

1. Single Pile: Only one pile of bananas

2. Large Piles: Very large numbers in piles

3. Minimum Hours: h = number of piles (must eat one pile per hour)

4. Maximum Hours: h = sum of all piles (can eat 1 banana per hour)

5. No Solution: When it's impossible to finish in time

Solution

⚠️ Disclaimer: Don't jump directly to the solution, try it out yourself first. Only look at the solution after you've given it a good attempt.

Solution 1: Binary Search (Optimal)

Time Complexity: O(n * log M) where n is number of piles and M is max pile size

Space Complexity: O(1) - Constant extra space

Solution 2: Linear Search

Time Complexity: O(n * M) where M is max pile size

Space Complexity: O(1) - Constant extra space

Solution 3: Mathematical Approach

Time Complexity: O(n) - Single pass through piles

Space Complexity: O(1) - Constant extra space

More Accenture coding questions