875. Koko Eating Bananas

Koko Eating Bananas (LeetCode 875) is a Medium Data Structures and Algorithms interview problem you can solve, run and submit on Thita.ai. It belongs to the Binary Search Patterns pattern, in the On Answer / Condition Function subpattern.

How to solve Koko Eating Bananas

Koko Eating Bananas is a Binary Search on Answer Space problem, part of Binary Search Patterns. Learn binary search on answer space for optimization problems like Koko eating bananas and capacity to ship packages.

Work towards the solution in these steps:

  1. Think about how you can use binary search to find an optimal value when the answer lies within a numeric range.
  2. Identify the search space for the eating speed: what are the minimum and maximum possible speeds Koko could have?
  3. Define a helper function that, given a speed, calculates the total hours Koko needs to finish all piles at that speed.

The reference solution runs in O(n * log M) time and O(1) space.

The Koko Eating Bananas solution and editorial walks through the full approach with reference code.

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

Constraints:

- 1 <= piles.length <= 10^4

- piles.length <= h <= 10^9

- 1 <= piles[i] <= 10^9

Topics and companies

Topics: Array, Binary Search.

Reported in interviews at Airbnb, Amazon, Facebook, Google.

How to practise Koko Eating Bananas on Thita.ai

Starter code is provided in C, C#, C++, Go, Java, JavaScript and Python. Your solution runs against 5 test cases for this problem, with the AI coach available for a hint when you are stuck rather than a finished answer. The reference solution runs in O(n * log M) time and O(1) space.

Open Koko Eating Bananas in the code editor, or read the Koko Eating Bananas editorial for a worked solution with its approach and complexity analysis.

Where Koko Eating Bananas sits in the DSA pattern sheet

Problems related to Koko Eating Bananas

Other problems that use the same On Answer / Condition Function and Binary Search Patterns techniques:

Koko Eating Bananas FAQ

What is the LeetCode number of Koko Eating Bananas?

Koko Eating Bananas is LeetCode problem 875, rated Medium.

What pattern is used to solve Koko Eating Bananas?

Koko Eating Bananas is a Binary Search on Answer Space problem. It belongs to the Binary Search Patterns pattern, in the On Answer / Condition Function subpattern.

What is the time complexity of Koko Eating Bananas?

The reference solution runs in O(n * log M) time and O(1) space.

Which companies ask Koko Eating Bananas?

Koko Eating Bananas has been reported in interviews at Airbnb, Amazon, Facebook, Google.

Preparing this workspace.