{"id":587,"date":"2025-07-09T18:37:53","date_gmt":"2025-07-09T13:07:53","guid":{"rendered":"https:\/\/codeanddebug.in\/blog\/?p=587"},"modified":"2025-07-09T18:37:55","modified_gmt":"2025-07-09T13:07:55","slug":"koko-eating-bananas-leetcode-875","status":"publish","type":"post","link":"https:\/\/codeanddebug.in\/blog\/koko-eating-bananas-leetcode-875\/","title":{"rendered":"Koko Eating Bananas | Leetcode 875 | Binary Search on Answers"},"content":{"rendered":"\n<p>The &#8220;Koko Eating Bananas&#8221; problem&nbsp;is a popular&nbsp;binary search&nbsp;interview question&nbsp;that tests your&nbsp;ability to optimize&nbsp;under constraints.<\/p>\n\n\n\n<p>In this blog, we\u2019ll explain the problem, walk you through both brute force and optimal solutions, and make everything easy to understand with code comments, dry runs, and clear explanations.<\/p>\n\n\n\n<p>Here&#8217;s the [<strong><a href=\"https:\/\/leetcode.com\/problems\/koko-eating-bananas\/description\/\" target=\"_blank\" rel=\"noreferrer noopener\"><mark style=\"background-color:rgba(0, 0, 0, 0)\" class=\"has-inline-color has-vivid-purple-color\"><span style=\"text-decoration: underline;\">Problem Link<\/span><\/mark><\/a><\/strong>] to begin with.<\/p>\n\n\n\n<h3 class=\"wp-block-heading\" id=\"0-problem-statement\">Problem Statement<\/h3>\n\n\n\n<p>Koko loves to eat bananas. There are&nbsp;<code>n<\/code>&nbsp;piles of bananas, the&nbsp;<code>i<sup>th<\/sup><\/code>&nbsp;pile has&nbsp;<code>piles[i]<\/code>&nbsp;bananas. The guards have gone and will come back in&nbsp;<code>h<\/code>&nbsp;hours.<\/p>\n\n\n\n<p>Koko can decide her bananas-per-hour eating speed of&nbsp;<code>k<\/code>. Each hour, she chooses some pile of bananas and eats&nbsp;<code>k<\/code>&nbsp;bananas from that pile. If the pile has less than&nbsp;<code>k<\/code>&nbsp;bananas, she eats all of them instead and will not eat any more bananas during this hour.<\/p>\n\n\n\n<p>Koko likes to eat slowly but still wants to finish eating all the bananas before the guards return.<\/p>\n\n\n\n<p>Return&nbsp;<em>the minimum integer<\/em>&nbsp;<code>k<\/code>&nbsp;<em>such that she can eat all the bananas within<\/em>&nbsp;<code>h<\/code>&nbsp;<em>hours<\/em>.<\/p>\n\n\n\n<p><strong>Example 1:<\/strong><\/p>\n\n\n\n<pre class=\"wp-block-preformatted\"><strong>Input:<\/strong> piles = [3,6,7,11], h = 8<br><strong>Output:<\/strong> 4<\/pre>\n\n\n\n<p><strong>Example 2:<\/strong><\/p>\n\n\n\n<pre class=\"wp-block-preformatted\"><strong>Input:<\/strong> piles = [30,11,23,4,20], h = 5<br><strong>Output:<\/strong> 30<\/pre>\n\n\n\n<p><strong>Example 3:<\/strong><\/p>\n\n\n\n<pre class=\"wp-block-preformatted\"><strong>Input:<\/strong> piles = [30,11,23,4,20], h = 6<br><strong>Output:<\/strong> 23<\/pre>\n\n\n\n<p><strong>Constraints:<\/strong><\/p>\n\n\n\n<ul class=\"wp-block-list\">\n<li><code>1 &lt;= piles.length &lt;= 10<sup>4<\/sup><\/code><\/li>\n\n\n\n<li><code>piles.length &lt;= h &lt;= 10<sup>9<\/sup><\/code><\/li>\n\n\n\n<li><code>1 &lt;= piles[i] &lt;= 10<sup>9<\/sup><\/code><\/li>\n<\/ul>\n\n\n\n<hr class=\"wp-block-separator has-alpha-channel-opacity\"\/>\n\n\n\n<h2 class=\"wp-block-heading\" id=\"1-brute-force-solution-try-every-possible-speed\">Brute Force Solution (Try Every Possible Speed)<\/h2>\n\n\n\n<h3 class=\"wp-block-heading\" id=\"2-intuition-and-approach\">Intuition and Approach<\/h3>\n\n\n\n<p>Let\u2019s solve the problem in the most straightforward way:<\/p>\n\n\n\n<ol class=\"wp-block-list\">\n<li><strong>Try Every Speed:<\/strong><br>Try every possible eating speed from 1 up to the largest pile.<\/li>\n\n\n\n<li><strong>Calculate Total Hours:<\/strong><br>For each speed, calculate the total hours Koko would take to eat all bananas.<\/li>\n\n\n\n<li><strong>Return the First Valid Speed:<\/strong><br>The first speed for which total hours is less than or equal to\u00a0<code>h<\/code>\u00a0is our answer.<\/li>\n\n\n\n<li><strong>Why does this work?<\/strong><br>We check every possible speed, so we\u2019re sure to find the minimum one that works.<\/li>\n<\/ol>\n\n\n\n<p>This approach is easy to understand but not efficient for large piles or high values.<\/p>\n\n\n\n<h3 class=\"wp-block-heading\" id=\"3-code-implementation\">Code Implementation<\/h3>\n\n\n\n<div class=\"wp-block-kevinbatdorf-code-block-pro padding-bottom-disabled cbp-has-line-numbers\" data-code-block-pro-font-family=\"Code-Pro-JetBrains-Mono\" style=\"font-size:.875rem;font-family:Code-Pro-JetBrains-Mono,ui-monospace,SFMono-Regular,Menlo,Monaco,Consolas,monospace;--cbp-line-number-color:#e1e4e8;--cbp-line-number-width:calc(2 * 0.6 * .875rem);line-height:1.25rem;--cbp-tab-width:2;tab-size:var(--cbp-tab-width, 2)\"><span style=\"display:block;padding:16px 0 0 16px;margin-bottom:-1px;width:100%;text-align:left;background-color:#24292e\"><svg xmlns=\"http:\/\/www.w3.org\/2000\/svg\" width=\"54\" height=\"14\" viewBox=\"0 0 54 14\"><g fill=\"none\" fill-rule=\"evenodd\" transform=\"translate(1 1)\"><circle cx=\"6\" cy=\"6\" r=\"6\" fill=\"#FF5F56\" stroke=\"#E0443E\" stroke-width=\".5\"><\/circle><circle cx=\"26\" cy=\"6\" r=\"6\" fill=\"#FFBD2E\" stroke=\"#DEA123\" stroke-width=\".5\"><\/circle><circle cx=\"46\" cy=\"6\" r=\"6\" fill=\"#27C93F\" stroke=\"#1AAB29\" stroke-width=\".5\"><\/circle><\/g><\/svg><\/span><span role=\"button\" tabindex=\"0\" data-code=\"import math\nfrom typing import List\n\nclass Solution:\n    def totalHours(self, piles, hourly_rate):\n        total = 0\n        for i in range(len(piles)):\n            total = total + math.ceil(piles[i] \/ hourly_rate)  # Calculate hours for each pile\n        return total\n\n    def minEatingSpeed(self, piles: List[int], h: int) -&gt; int:\n        maximum_banana = max(piles)             # The highest possible eating speed\n        for i in range(1, maximum_banana + 1):  # Try every possible speed\n            total_hour = self.totalHours(piles, i)\n            if total_hour &lt;= h:                 # If Koko can finish in h hours\n                return i                        # Return this speed\" style=\"color:#e1e4e8;display:none\" aria-label=\"Copy\" class=\"code-block-pro-copy-button\"><svg xmlns=\"http:\/\/www.w3.org\/2000\/svg\" style=\"width:24px;height:24px\" fill=\"none\" viewBox=\"0 0 24 24\" stroke=\"currentColor\" stroke-width=\"2\"><path class=\"with-check\" stroke-linecap=\"round\" stroke-linejoin=\"round\" d=\"M9 5H7a2 2 0 00-2 2v12a2 2 0 002 2h10a2 2 0 002-2V7a2 2 0 00-2-2h-2M9 5a2 2 0 002 2h2a2 2 0 002-2M9 5a2 2 0 012-2h2a2 2 0 012 2m-6 9l2 2 4-4\"><\/path><path class=\"without-check\" stroke-linecap=\"round\" stroke-linejoin=\"round\" d=\"M9 5H7a2 2 0 00-2 2v12a2 2 0 002 2h10a2 2 0 002-2V7a2 2 0 00-2-2h-2M9 5a2 2 0 002 2h2a2 2 0 002-2M9 5a2 2 0 012-2h2a2 2 0 012 2\"><\/path><\/svg><\/span><pre class=\"shiki github-dark\" style=\"background-color: #24292e\" tabindex=\"0\"><code><span class=\"line\"><span style=\"color: #F97583\">import<\/span><span style=\"color: #E1E4E8\"> math<\/span><\/span>\n<span class=\"line\"><span style=\"color: #F97583\">from<\/span><span style=\"color: #E1E4E8\"> typing <\/span><span style=\"color: #F97583\">import<\/span><span style=\"color: #E1E4E8\"> List<\/span><\/span>\n<span class=\"line\"><\/span>\n<span class=\"line\"><span style=\"color: #F97583\">class<\/span><span style=\"color: #E1E4E8\"> <\/span><span style=\"color: #B392F0\">Solution<\/span><span style=\"color: #E1E4E8\">:<\/span><\/span>\n<span class=\"line\"><span style=\"color: #E1E4E8\">    <\/span><span style=\"color: #F97583\">def<\/span><span style=\"color: #E1E4E8\"> <\/span><span style=\"color: #B392F0\">totalHours<\/span><span style=\"color: #E1E4E8\">(self, piles, hourly_rate):<\/span><\/span>\n<span class=\"line\"><span style=\"color: #E1E4E8\">        total <\/span><span style=\"color: #F97583\">=<\/span><span style=\"color: #E1E4E8\"> <\/span><span style=\"color: #79B8FF\">0<\/span><\/span>\n<span class=\"line\"><span style=\"color: #E1E4E8\">        <\/span><span style=\"color: #F97583\">for<\/span><span style=\"color: #E1E4E8\"> i <\/span><span style=\"color: #F97583\">in<\/span><span style=\"color: #E1E4E8\"> <\/span><span style=\"color: #79B8FF\">range<\/span><span style=\"color: #E1E4E8\">(<\/span><span style=\"color: #79B8FF\">len<\/span><span style=\"color: #E1E4E8\">(piles)):<\/span><\/span>\n<span class=\"line\"><span style=\"color: #E1E4E8\">            total <\/span><span style=\"color: #F97583\">=<\/span><span style=\"color: #E1E4E8\"> total <\/span><span style=\"color: #F97583\">+<\/span><span style=\"color: #E1E4E8\"> math.ceil(piles[i] <\/span><span style=\"color: #F97583\">\/<\/span><span style=\"color: #E1E4E8\"> hourly_rate)  <\/span><span style=\"color: #6A737D\"># Calculate hours for each pile<\/span><\/span>\n<span class=\"line\"><span style=\"color: #E1E4E8\">        <\/span><span style=\"color: #F97583\">return<\/span><span style=\"color: #E1E4E8\"> total<\/span><\/span>\n<span class=\"line\"><\/span>\n<span class=\"line\"><span style=\"color: #E1E4E8\">    <\/span><span style=\"color: #F97583\">def<\/span><span style=\"color: #E1E4E8\"> <\/span><span style=\"color: #B392F0\">minEatingSpeed<\/span><span style=\"color: #E1E4E8\">(self, piles: List[<\/span><span style=\"color: #79B8FF\">int<\/span><span style=\"color: #E1E4E8\">], h: <\/span><span style=\"color: #79B8FF\">int<\/span><span style=\"color: #E1E4E8\">) -&gt; <\/span><span style=\"color: #79B8FF\">int<\/span><span style=\"color: #E1E4E8\">:<\/span><\/span>\n<span class=\"line\"><span style=\"color: #E1E4E8\">        maximum_banana <\/span><span style=\"color: #F97583\">=<\/span><span style=\"color: #E1E4E8\"> <\/span><span style=\"color: #79B8FF\">max<\/span><span style=\"color: #E1E4E8\">(piles)             <\/span><span style=\"color: #6A737D\"># The highest possible eating speed<\/span><\/span>\n<span class=\"line\"><span style=\"color: #E1E4E8\">        <\/span><span style=\"color: #F97583\">for<\/span><span style=\"color: #E1E4E8\"> i <\/span><span style=\"color: #F97583\">in<\/span><span style=\"color: #E1E4E8\"> <\/span><span style=\"color: #79B8FF\">range<\/span><span style=\"color: #E1E4E8\">(<\/span><span style=\"color: #79B8FF\">1<\/span><span style=\"color: #E1E4E8\">, maximum_banana <\/span><span style=\"color: #F97583\">+<\/span><span style=\"color: #E1E4E8\"> <\/span><span style=\"color: #79B8FF\">1<\/span><span style=\"color: #E1E4E8\">):  <\/span><span style=\"color: #6A737D\"># Try every possible speed<\/span><\/span>\n<span class=\"line\"><span style=\"color: #E1E4E8\">            total_hour <\/span><span style=\"color: #F97583\">=<\/span><span style=\"color: #E1E4E8\"> <\/span><span style=\"color: #79B8FF\">self<\/span><span style=\"color: #E1E4E8\">.totalHours(piles, i)<\/span><\/span>\n<span class=\"line\"><span style=\"color: #E1E4E8\">            <\/span><span style=\"color: #F97583\">if<\/span><span style=\"color: #E1E4E8\"> total_hour <\/span><span style=\"color: #F97583\">&lt;=<\/span><span style=\"color: #E1E4E8\"> h:                 <\/span><span style=\"color: #6A737D\"># If Koko can finish in h hours<\/span><\/span>\n<span class=\"line\"><span style=\"color: #E1E4E8\">                <\/span><span style=\"color: #F97583\">return<\/span><span style=\"color: #E1E4E8\"> i                        <\/span><span style=\"color: #6A737D\"># Return this speed<\/span><\/span><\/code><\/pre><span style=\"display:flex;align-items:flex-end;padding:10px;width:100%;justify-content:flex-end;background-color:#24292e;color:#d3d7dd;font-size:12px;line-height:1;position:relative\">Python<\/span><\/div>\n\n\n\n<h3 class=\"wp-block-heading\" id=\"4-code-explanation\">Code Explanation<\/h3>\n\n\n\n<ul class=\"wp-block-list\">\n<li>For each speed from 1 to the maximum pile size, calculate total hours needed.<\/li>\n\n\n\n<li>If total hours is within\u00a0<code>h<\/code>, return that speed.<\/li>\n<\/ul>\n\n\n\n<h3 class=\"wp-block-heading\" id=\"5-dry-run\">Dry Run<\/h3>\n\n\n\n<p><strong>Input:<\/strong><br><code>piles = , h = 8<\/code><\/p>\n\n\n\n<ul class=\"wp-block-list\">\n<li>Try k = 1: total hours = 3+6+7+11 = 27 (too high)<\/li>\n\n\n\n<li>Try k = 2: total hours = 2+3+4+6 = 15 (too high)<\/li>\n\n\n\n<li>Try k = 3: total hours = 1+2+3+4 = 10 (too high)<\/li>\n\n\n\n<li>Try k = 4: total hours = 1+2+2+3 = 8 (just right, return 4)<\/li>\n<\/ul>\n\n\n\n<p><strong>Final Output:<\/strong><br><code>4<\/code><\/p>\n\n\n\n<h3 class=\"wp-block-heading\" id=\"6-time-and-space-complexity\">Time and Space Complexity<\/h3>\n\n\n\n<ul class=\"wp-block-list\">\n<li><strong>Time Complexity:<\/strong>\u00a0O(max(piles) * n)<br>For each speed, we check all piles.<\/li>\n\n\n\n<li><strong>Space Complexity:<\/strong>\u00a0O(1)<br>Only variables for counting.<\/li>\n<\/ul>\n\n\n\n<h3 class=\"wp-block-heading\" id=\"7-conclusion\">Conclusion<\/h3>\n\n\n\n<p>The brute force approach is simple and works for small inputs, but it\u2019s too slow for large values.<\/p>\n\n\n\n<hr class=\"wp-block-separator has-alpha-channel-opacity\"\/>\n\n\n\n<h2 class=\"wp-block-heading\" id=\"8-optimal-solution-binary-search\">Optimal Solution (Binary Search)<\/h2>\n\n\n\n<h3 class=\"wp-block-heading\" id=\"9-intuition-and-approach\">Intuition and Approach<\/h3>\n\n\n\n<p>We can do much better using binary search:<\/p>\n\n\n\n<ol class=\"wp-block-list\">\n<li><strong>Binary Search on Speed:<\/strong><br>The answer lies between 1 and the largest pile size. Use binary search to find the minimum speed.<\/li>\n\n\n\n<li><strong>Check if Speed is Enough:<\/strong><br>For each mid value, calculate if Koko can finish in\u00a0<code>h<\/code>\u00a0hours.<\/li>\n\n\n\n<li><strong>Adjust Search Window:<\/strong>\n<ul class=\"wp-block-list\">\n<li>If she can finish, try a smaller speed.<\/li>\n\n\n\n<li>If not, try a larger speed.<\/li>\n<\/ul>\n<\/li>\n\n\n\n<li><strong>Why does this work?<\/strong><br>The total hours function is monotonic: as speed increases, hours decrease. Binary search is perfect for this.<\/li>\n<\/ol>\n\n\n\n<h3 class=\"wp-block-heading\" id=\"10-code-implementation\">Code Implementation<\/h3>\n\n\n\n<div class=\"wp-block-kevinbatdorf-code-block-pro padding-bottom-disabled cbp-has-line-numbers\" data-code-block-pro-font-family=\"Code-Pro-JetBrains-Mono\" style=\"font-size:.875rem;font-family:Code-Pro-JetBrains-Mono,ui-monospace,SFMono-Regular,Menlo,Monaco,Consolas,monospace;--cbp-line-number-color:#e1e4e8;--cbp-line-number-width:calc(2 * 0.6 * .875rem);line-height:1.25rem;--cbp-tab-width:2;tab-size:var(--cbp-tab-width, 2)\"><span style=\"display:block;padding:16px 0 0 16px;margin-bottom:-1px;width:100%;text-align:left;background-color:#24292e\"><svg xmlns=\"http:\/\/www.w3.org\/2000\/svg\" width=\"54\" height=\"14\" viewBox=\"0 0 54 14\"><g fill=\"none\" fill-rule=\"evenodd\" transform=\"translate(1 1)\"><circle cx=\"6\" cy=\"6\" r=\"6\" fill=\"#FF5F56\" stroke=\"#E0443E\" stroke-width=\".5\"><\/circle><circle cx=\"26\" cy=\"6\" r=\"6\" fill=\"#FFBD2E\" stroke=\"#DEA123\" stroke-width=\".5\"><\/circle><circle cx=\"46\" cy=\"6\" r=\"6\" fill=\"#27C93F\" stroke=\"#1AAB29\" stroke-width=\".5\"><\/circle><\/g><\/svg><\/span><span role=\"button\" tabindex=\"0\" data-code=\"class Solution:\n    def totalHours(self, piles, hourly_rate):\n        total = 0\n        for i in range(len(piles)):\n            total = total + math.ceil(piles[i] \/ hourly_rate)  # Calculate hours for each pile\n        return total\n\n    def minEatingSpeed(self, piles: List[int], h: int) -&gt; int:\n        low = 1\n        high = max(piles)\n        ans = -1\n        while low &lt;= high:\n            mid = (low + high) \/\/ 2\n            total_hours = self.totalHours(piles, mid)\n            if total_hours &lt;= h:\n                ans = mid                  # Try to find a smaller valid speed\n                high = mid - 1\n            else:\n                low = mid + 1              # Need a higher speed\n        return ans\" style=\"color:#e1e4e8;display:none\" aria-label=\"Copy\" class=\"code-block-pro-copy-button\"><svg xmlns=\"http:\/\/www.w3.org\/2000\/svg\" style=\"width:24px;height:24px\" fill=\"none\" viewBox=\"0 0 24 24\" stroke=\"currentColor\" stroke-width=\"2\"><path class=\"with-check\" stroke-linecap=\"round\" stroke-linejoin=\"round\" d=\"M9 5H7a2 2 0 00-2 2v12a2 2 0 002 2h10a2 2 0 002-2V7a2 2 0 00-2-2h-2M9 5a2 2 0 002 2h2a2 2 0 002-2M9 5a2 2 0 012-2h2a2 2 0 012 2m-6 9l2 2 4-4\"><\/path><path class=\"without-check\" stroke-linecap=\"round\" stroke-linejoin=\"round\" d=\"M9 5H7a2 2 0 00-2 2v12a2 2 0 002 2h10a2 2 0 002-2V7a2 2 0 00-2-2h-2M9 5a2 2 0 002 2h2a2 2 0 002-2M9 5a2 2 0 012-2h2a2 2 0 012 2\"><\/path><\/svg><\/span><pre class=\"shiki github-dark\" style=\"background-color: #24292e\" tabindex=\"0\"><code><span class=\"line\"><span style=\"color: #F97583\">class<\/span><span style=\"color: #E1E4E8\"> <\/span><span style=\"color: #B392F0\">Solution<\/span><span style=\"color: #E1E4E8\">:<\/span><\/span>\n<span class=\"line\"><span style=\"color: #E1E4E8\">    <\/span><span style=\"color: #F97583\">def<\/span><span style=\"color: #E1E4E8\"> <\/span><span style=\"color: #B392F0\">totalHours<\/span><span style=\"color: #E1E4E8\">(self, piles, hourly_rate):<\/span><\/span>\n<span class=\"line\"><span style=\"color: #E1E4E8\">        total <\/span><span style=\"color: #F97583\">=<\/span><span style=\"color: #E1E4E8\"> <\/span><span style=\"color: #79B8FF\">0<\/span><\/span>\n<span class=\"line\"><span style=\"color: #E1E4E8\">        <\/span><span style=\"color: #F97583\">for<\/span><span style=\"color: #E1E4E8\"> i <\/span><span style=\"color: #F97583\">in<\/span><span style=\"color: #E1E4E8\"> <\/span><span style=\"color: #79B8FF\">range<\/span><span style=\"color: #E1E4E8\">(<\/span><span style=\"color: #79B8FF\">len<\/span><span style=\"color: #E1E4E8\">(piles)):<\/span><\/span>\n<span class=\"line\"><span style=\"color: #E1E4E8\">            total <\/span><span style=\"color: #F97583\">=<\/span><span style=\"color: #E1E4E8\"> total <\/span><span style=\"color: #F97583\">+<\/span><span style=\"color: #E1E4E8\"> math.ceil(piles[i] <\/span><span style=\"color: #F97583\">\/<\/span><span style=\"color: #E1E4E8\"> hourly_rate)  <\/span><span style=\"color: #6A737D\"># Calculate hours for each pile<\/span><\/span>\n<span class=\"line\"><span style=\"color: #E1E4E8\">        <\/span><span style=\"color: #F97583\">return<\/span><span style=\"color: #E1E4E8\"> total<\/span><\/span>\n<span class=\"line\"><\/span>\n<span class=\"line\"><span style=\"color: #E1E4E8\">    <\/span><span style=\"color: #F97583\">def<\/span><span style=\"color: #E1E4E8\"> <\/span><span style=\"color: #B392F0\">minEatingSpeed<\/span><span style=\"color: #E1E4E8\">(self, piles: List[<\/span><span style=\"color: #79B8FF\">int<\/span><span style=\"color: #E1E4E8\">], h: <\/span><span style=\"color: #79B8FF\">int<\/span><span style=\"color: #E1E4E8\">) -&gt; <\/span><span style=\"color: #79B8FF\">int<\/span><span style=\"color: #E1E4E8\">:<\/span><\/span>\n<span class=\"line\"><span style=\"color: #E1E4E8\">        low <\/span><span style=\"color: #F97583\">=<\/span><span style=\"color: #E1E4E8\"> <\/span><span style=\"color: #79B8FF\">1<\/span><\/span>\n<span class=\"line\"><span style=\"color: #E1E4E8\">        high <\/span><span style=\"color: #F97583\">=<\/span><span style=\"color: #E1E4E8\"> <\/span><span style=\"color: #79B8FF\">max<\/span><span style=\"color: #E1E4E8\">(piles)<\/span><\/span>\n<span class=\"line\"><span style=\"color: #E1E4E8\">        ans <\/span><span style=\"color: #F97583\">=<\/span><span style=\"color: #E1E4E8\"> <\/span><span style=\"color: #F97583\">-<\/span><span style=\"color: #79B8FF\">1<\/span><\/span>\n<span class=\"line\"><span style=\"color: #E1E4E8\">        <\/span><span style=\"color: #F97583\">while<\/span><span style=\"color: #E1E4E8\"> low <\/span><span style=\"color: #F97583\">&lt;=<\/span><span style=\"color: #E1E4E8\"> high:<\/span><\/span>\n<span class=\"line\"><span style=\"color: #E1E4E8\">            mid <\/span><span style=\"color: #F97583\">=<\/span><span style=\"color: #E1E4E8\"> (low <\/span><span style=\"color: #F97583\">+<\/span><span style=\"color: #E1E4E8\"> high) <\/span><span style=\"color: #F97583\">\/\/<\/span><span style=\"color: #E1E4E8\"> <\/span><span style=\"color: #79B8FF\">2<\/span><\/span>\n<span class=\"line\"><span style=\"color: #E1E4E8\">            total_hours <\/span><span style=\"color: #F97583\">=<\/span><span style=\"color: #E1E4E8\"> <\/span><span style=\"color: #79B8FF\">self<\/span><span style=\"color: #E1E4E8\">.totalHours(piles, mid)<\/span><\/span>\n<span class=\"line\"><span style=\"color: #E1E4E8\">            <\/span><span style=\"color: #F97583\">if<\/span><span style=\"color: #E1E4E8\"> total_hours <\/span><span style=\"color: #F97583\">&lt;=<\/span><span style=\"color: #E1E4E8\"> h:<\/span><\/span>\n<span class=\"line\"><span style=\"color: #E1E4E8\">                ans <\/span><span style=\"color: #F97583\">=<\/span><span style=\"color: #E1E4E8\"> mid                  <\/span><span style=\"color: #6A737D\"># Try to find a smaller valid speed<\/span><\/span>\n<span class=\"line\"><span style=\"color: #E1E4E8\">                high <\/span><span style=\"color: #F97583\">=<\/span><span style=\"color: #E1E4E8\"> mid <\/span><span style=\"color: #F97583\">-<\/span><span style=\"color: #E1E4E8\"> <\/span><span style=\"color: #79B8FF\">1<\/span><\/span>\n<span class=\"line\"><span style=\"color: #E1E4E8\">            <\/span><span style=\"color: #F97583\">else<\/span><span style=\"color: #E1E4E8\">:<\/span><\/span>\n<span class=\"line\"><span style=\"color: #E1E4E8\">                low <\/span><span style=\"color: #F97583\">=<\/span><span style=\"color: #E1E4E8\"> mid <\/span><span style=\"color: #F97583\">+<\/span><span style=\"color: #E1E4E8\"> <\/span><span style=\"color: #79B8FF\">1<\/span><span style=\"color: #E1E4E8\">              <\/span><span style=\"color: #6A737D\"># Need a higher speed<\/span><\/span>\n<span class=\"line\"><span style=\"color: #E1E4E8\">        <\/span><span style=\"color: #F97583\">return<\/span><span style=\"color: #E1E4E8\"> ans<\/span><\/span><\/code><\/pre><span style=\"display:flex;align-items:flex-end;padding:10px;width:100%;justify-content:flex-end;background-color:#24292e;color:#d3d7dd;font-size:12px;line-height:1;position:relative\">Python<\/span><\/div>\n\n\n\n<h3 class=\"wp-block-heading\" id=\"11-code-explanation\">Code Explanation<\/h3>\n\n\n\n<ul class=\"wp-block-list\">\n<li>Use binary search between 1 and max pile size.<\/li>\n\n\n\n<li>For each mid speed, calculate total hours needed.<\/li>\n\n\n\n<li>If total hours is within\u00a0<code>h<\/code>, store the speed and try smaller.<\/li>\n\n\n\n<li>If not, try higher speeds.<\/li>\n\n\n\n<li>Return the minimum valid speed found.<\/li>\n<\/ul>\n\n\n\n<h3 class=\"wp-block-heading\" id=\"12-dry-run\">Dry Run<\/h3>\n\n\n\n<p><strong>Input:<\/strong><br><code>piles = , h = 8<\/code><\/p>\n\n\n\n<ul class=\"wp-block-list\">\n<li>low = 1, high = 11<\/li>\n\n\n\n<li>mid = 6, total_hours = 1+1+2+2 = 6 (can finish, try smaller speed)<\/li>\n\n\n\n<li>high = 5<\/li>\n\n\n\n<li>mid = 3, total_hours = 1+2+3+4 = 10 (too slow, try faster)<\/li>\n\n\n\n<li>low = 4<\/li>\n\n\n\n<li>mid = 4, total_hours = 1+2+2+3 = 8 (just right, try even smaller)<\/li>\n\n\n\n<li>high = 3<\/li>\n<\/ul>\n\n\n\n<p>Loop ends, answer is 4.<\/p>\n\n\n\n<h3 class=\"wp-block-heading\" id=\"13-time-and-space-complexity\">Time and Space Complexity<\/h3>\n\n\n\n<ul class=\"wp-block-list\">\n<li><strong>Time Complexity:<\/strong>\u00a0O(n * log(max(piles)))<br>Binary search on speed, checking all piles for each guess.<\/li>\n\n\n\n<li><strong>Space Complexity:<\/strong>\u00a0O(1)<br>Only variables for counting.<\/li>\n<\/ul>\n\n\n\n<h3 class=\"wp-block-heading\" id=\"14-conclusion\">Conclusion<\/h3>\n\n\n\n<p>The optimal solution is efficient and works well for large arrays and high values. It\u2019s the best approach for interviews and practical use.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\" id=\"15-final-thoughts\">Final Thoughts<\/h2>\n\n\n\n<p>The &#8220;Koko Eating Bananas&#8221; problem is a great example of how to optimize from brute force to an efficient binary search solution. Start with brute force to understand the basics, then use binary search for best performance.<\/p>\n\n\n\n<div class=\"wp-block-buttons is-content-justification-center is-layout-flex wp-container-core-buttons-is-layout-16018d1d wp-block-buttons-is-layout-flex\">\n<div class=\"wp-block-button\"><a class=\"wp-block-button__link wp-element-button\" href=\"https:\/\/codeanddebug.in\/course\/zero-to-hero-python-dsa\" target=\"_blank\" rel=\"noreferrer noopener\">Join our Advance DSA COURSE<\/a><\/div>\n<\/div>\n\n\n\n<hr class=\"wp-block-separator has-alpha-channel-opacity\"\/>\n\n\n\n<p><em>For any changes to the article, kindly email at <a href=\"mailto:code@codeanddebug.in\" target=\"_blank\" rel=\"noreferrer noopener\">code@codeanddebug.in<\/a> or contact us at <a href=\"tel:+91-9712928220\" target=\"_blank\" rel=\"noreferrer noopener\">+91-9712928220<\/a>.<\/em><\/p>\n","protected":false},"excerpt":{"rendered":"<p>The &#8220;Koko Eating Bananas&#8221; problem&nbsp;is a popular&nbsp;binary search&nbsp;interview question&nbsp;that tests your&nbsp;ability to optimize&nbsp;under constraints. In this blog, we\u2019ll explain the problem, walk you through both brute force and optimal solutions, and make everything easy to understand with code comments, dry runs, and clear explanations. Here&#8217;s the [Problem Link] to begin with. Problem Statement Koko loves<\/p>\n","protected":false},"author":1,"featured_media":588,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[3,6],"tags":[28,19],"class_list":{"0":"post-587","1":"post","2":"type-post","3":"status-publish","4":"format-standard","5":"has-post-thumbnail","7":"category-data-structures-and-algorithm","8":"category-intermediate","9":"tag-binary-search-on-answers","10":"tag-medium"},"featured_image_src":"https:\/\/codeanddebug.in\/blog\/wp-content\/uploads\/2025\/07\/koko-eating-bananas-featured-image.png","author_info":{"display_name":"codeanddebug","author_link":"https:\/\/codeanddebug.in\/blog\/author\/codeanddebug\/"},"_links":{"self":[{"href":"https:\/\/codeanddebug.in\/blog\/wp-json\/wp\/v2\/posts\/587","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/codeanddebug.in\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/codeanddebug.in\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/codeanddebug.in\/blog\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/codeanddebug.in\/blog\/wp-json\/wp\/v2\/comments?post=587"}],"version-history":[{"count":1,"href":"https:\/\/codeanddebug.in\/blog\/wp-json\/wp\/v2\/posts\/587\/revisions"}],"predecessor-version":[{"id":589,"href":"https:\/\/codeanddebug.in\/blog\/wp-json\/wp\/v2\/posts\/587\/revisions\/589"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/codeanddebug.in\/blog\/wp-json\/wp\/v2\/media\/588"}],"wp:attachment":[{"href":"https:\/\/codeanddebug.in\/blog\/wp-json\/wp\/v2\/media?parent=587"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/codeanddebug.in\/blog\/wp-json\/wp\/v2\/categories?post=587"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/codeanddebug.in\/blog\/wp-json\/wp\/v2\/tags?post=587"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}