Advertisement

Koko Eating Bananas - LeetCode 875 Solution

Koko Eating Bananas - Complete Solution Guide

Koko Eating Bananas is LeetCode problem 875, a Medium level challenge. This complete guide provides step-by-step explanations, multiple solution approaches, and optimized code in python3, java, cpp, c.

Problem Statement

Koko loves to eat bananas. There are n piles of bananas, the i th 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. R

Detailed Explanation

Koko loves to eat bananas. She has `n` piles of bananas, where the `i`-th pile has `piles[i]` bananas. The guards are returning in `h` hours. Koko needs to eat all the bananas before the guards return. Koko can choose her eating speed `k` bananas per hour. If a pile has fewer than `k` bananas, she eats them all and doesn't eat from any other pile that hour. The problem asks for the minimum `k` such that Koko can eat all the bananas within `h` hours.

Solution Approach

The solution uses binary search to find the minimum eating speed `k`. The binary search space is from 1 to the maximum number of bananas in any pile. For each potential `k`, the algorithm calculates the total hours spent eating all the piles. If the total hours spent is less than or equal to `h`, it means `k` might be too high, and we search in the lower half. Otherwise, `k` is too low, and we search in the upper half. The binary search continues until the lower and upper bounds converge, giving us the minimum `k`.

Step-by-Step Algorithm

  1. Step 1: Find the maximum number of bananas in any pile. This will be the upper bound for the binary search.
  2. Step 2: Initialize the lower bound of the binary search to 1 and the upper bound to the maximum number of bananas found in Step 1.
  3. Step 3: While the lower bound is less than the upper bound, calculate the middle value `k` as the average of the lower and upper bounds (using `(low + high) // 2` to prevent overflow).
  4. Step 4: Calculate the total hours spent eating all the piles for the current value of `k`. For each pile, the hours spent is `(pile + k - 1) // k`.
  5. Step 5: If the total hours spent is less than or equal to `h`, it means `k` might be too high. Update the upper bound to `k`.
  6. Step 6: Otherwise, `k` is too low. Update the lower bound to `k + 1`.
  7. Step 7: After the while loop terminates, the lower bound will be equal to the minimum eating speed `k`.

Key Insights

  • Insight 1: The problem is essentially finding the minimum value of `k` within a specific range that satisfies the constraint of eating all bananas within `h` hours. This points towards using binary search.
  • Insight 2: The eating speed `k` is bounded. The lower bound is 1 (she must eat at least one banana per hour), and the upper bound is the maximum number of bananas in any pile (if she eats faster than this, she wastes time).
  • Insight 3: Calculating the time spent eating a pile can be done using `ceil(pile_size / k)`, but a common trick is to use integer division: `(pile_size + k - 1) // k`.

Complexity Analysis

Time Complexity: O(nlog(m))

Space Complexity: O(1)

Topics

This problem involves: Array, Binary Search.

Companies

Asked at: Accenture, Atlassian, Autodesk, Citadel, DE Shaw, Deutsche Bank, DoorDash, Flipkart, HashedIn, Infosys, Netflix, Nykaa, Okta, PayPal, PhonePe, Ripple, Splunk, Turing, VMware, Walmart Labs, Zepto, eBay, oyo.