<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0" xmlns:atom="http://www.w3.org/2005/Atom" xmlns:dc="http://purl.org/dc/elements/1.1/">
  <channel>
    <title>DEV Community: Jennifer </title>
    <description>The latest articles on DEV Community by Jennifer  (@jennthecoder).</description>
    <link>https://dev.to/jennthecoder</link>
    <image>
      <url>https://media2.dev.to/dynamic/image/width=90,height=90,fit=cover,gravity=auto,format=auto/https:%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Fuser%2Fprofile_image%2F1989593%2F7d5d2b88-9748-4df8-9d55-3c1dea2e4940.png</url>
      <title>DEV Community: Jennifer </title>
      <link>https://dev.to/jennthecoder</link>
    </image>
    <atom:link rel="self" type="application/rss+xml" href="https://dev.to/feed/jennthecoder"/>
    <language>en</language>
    <item>
      <title>Leetcode: minEatingSpeed</title>
      <dc:creator>Jennifer </dc:creator>
      <pubDate>Wed, 28 Aug 2024 02:18:08 +0000</pubDate>
      <link>https://dev.to/jennthecoder/leetcode-mineatingspeed-1j97</link>
      <guid>https://dev.to/jennthecoder/leetcode-mineatingspeed-1j97</guid>
      <description>&lt;h2&gt;
  
  
  Breaking Down Leetcode Problems: Koko Eating Bananas
&lt;/h2&gt;

&lt;p&gt;Hi, welcome to my new series where I break down Leetcode problems to help you understand the thought process. Let's dive straight into the problem.&lt;/p&gt;

&lt;h3&gt;
  
  
  The Problem
&lt;/h3&gt;

&lt;p&gt;Koko loves to eat bananas. There are &lt;code&gt;n&lt;/code&gt; piles of bananas, where the &lt;code&gt;i&lt;/code&gt;th pile has &lt;code&gt;piles[i]&lt;/code&gt; bananas. The guards have left and will return in &lt;code&gt;h&lt;/code&gt; hours.&lt;/p&gt;

&lt;p&gt;Koko can decide her bananas-per-hour eating speed, denoted by &lt;code&gt;k&lt;/code&gt;. Each hour, she chooses a pile and eats &lt;code&gt;k&lt;/code&gt; bananas. If the pile has fewer than &lt;code&gt;k&lt;/code&gt; bananas, she eats all of them and does not eat any more bananas during that hour.&lt;/p&gt;

&lt;p&gt;Your task is to return the minimum integer &lt;code&gt;k&lt;/code&gt; such that Koko can eat all the bananas within &lt;code&gt;h&lt;/code&gt; hours.&lt;/p&gt;

&lt;h3&gt;
  
  
  Let's Break It Down
&lt;/h3&gt;

&lt;p&gt;I admit, this problem took me some time to understand due to its wording.&lt;/p&gt;

&lt;p&gt;You are given an array of integers &lt;code&gt;piles&lt;/code&gt;, where each element represents a pile of bananas. You are also given &lt;code&gt;h&lt;/code&gt;, the total number of hours Koko has to eat all the bananas. Your goal is to find the &lt;strong&gt;minimum speed&lt;/strong&gt; &lt;code&gt;k&lt;/code&gt; at which Koko needs to eat to finish all the bananas within the given hours (&lt;code&gt;h&lt;/code&gt;).&lt;/p&gt;

&lt;p&gt;The problem also tells us that a pile of bananas (&lt;code&gt;piles[i]&lt;/code&gt;) could be smaller than Koko's eating speed (meaning she might eat faster than the number of bananas in a pile).&lt;/p&gt;

&lt;h3&gt;
  
  
  Sample Input
&lt;/h3&gt;

&lt;p&gt;Let's look at a sample input:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;
&lt;strong&gt;Input:&lt;/strong&gt; &lt;code&gt;piles = [3, 6, 7, 11]&lt;/code&gt;, &lt;code&gt;h = 8&lt;/code&gt;
&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Output:&lt;/strong&gt; &lt;code&gt;4&lt;/code&gt;
&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;We need to find the minimum speed &lt;code&gt;k&lt;/code&gt;, which can range from &lt;code&gt;1&lt;/code&gt; to the maximum number in the array (&lt;code&gt;11&lt;/code&gt; in this example).&lt;/p&gt;

&lt;h3&gt;
  
  
  Approach
&lt;/h3&gt;

&lt;p&gt;To solve the problem, we need to find the minimum speed that allows Koko to eat all the bananas within &lt;code&gt;h&lt;/code&gt; hours. Since we don't know &lt;code&gt;k&lt;/code&gt;, we can use binary search:&lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;
&lt;strong&gt;Guess &lt;code&gt;k&lt;/code&gt;&lt;/strong&gt;: Start with a midpoint in the range (&lt;code&gt;1&lt;/code&gt; to &lt;code&gt;max(piles)&lt;/code&gt;) and calculate the total hours required to eat all piles at that speed (&lt;code&gt;curr_h&lt;/code&gt;).&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Adjust the Range&lt;/strong&gt;:

&lt;ul&gt;
&lt;li&gt;If &lt;code&gt;curr_h &amp;gt; h&lt;/code&gt;, &lt;code&gt;k&lt;/code&gt; is too small. Increase &lt;code&gt;k&lt;/code&gt; by setting &lt;code&gt;low&lt;/code&gt; to &lt;code&gt;k + 1&lt;/code&gt;.&lt;/li&gt;
&lt;li&gt;If &lt;code&gt;curr_h &amp;lt;= h&lt;/code&gt;, &lt;code&gt;k&lt;/code&gt; might be sufficient. Decrease &lt;code&gt;k&lt;/code&gt; by setting &lt;code&gt;high&lt;/code&gt; to &lt;code&gt;k - 1&lt;/code&gt;.&lt;/li&gt;
&lt;/ul&gt;
&lt;/li&gt;
&lt;/ol&gt;

&lt;h3&gt;
  
  
  Solution Code
&lt;/h3&gt;

&lt;p&gt;Here's the Python implementation:&lt;/p&gt;



&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;
python
from math import ceil

def minEatingSpeed(piles, h):
    low, high = 1, max(piles)

    while low &amp;lt;= high:
        k = (low + high) // 2
        curr_h = sum(ceil(pile / k) for pile in piles)

        if curr_h &amp;gt; h:
            low = k + 1
        else:
            high = k - 1

    return low
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;

</description>
      <category>datastructures</category>
      <category>leetcode</category>
      <category>algorithms</category>
    </item>
  </channel>
</rss>
