{"id":3260,"date":"2018-07-22T04:04:14","date_gmt":"2018-07-22T11:04:14","guid":{"rendered":"http:\/\/zxi.mytechroad.com\/blog\/?p=3260"},"modified":"2018-07-22T04:15:29","modified_gmt":"2018-07-22T11:15:29","slug":"leetcode-875-koko-eating-bananas","status":"publish","type":"post","link":"https:\/\/zxi.mytechroad.com\/blog\/algorithms\/binary-search\/leetcode-875-koko-eating-bananas\/","title":{"rendered":"\u82b1\u82b1\u9171 LeetCode 875. Koko Eating Bananas"},"content":{"rendered":"<h1><strong>Problem<\/strong><\/h1>\n<p>Koko loves to eat bananas.\u00a0 There are\u00a0<code>N<\/code>\u00a0piles of bananas, the\u00a0<code>i<\/code>-th\u00a0pile has\u00a0<code>piles[i]<\/code>\u00a0bananas.\u00a0 The guards have gone and will come back in\u00a0<code>H<\/code>\u00a0hours.<\/p>\n<p>Koko can decide her bananas-per-hour eating speed of\u00a0<code>K<\/code>.\u00a0 Each hour, she chooses some pile of bananas, and eats K bananas from that pile.\u00a0 If the pile has less than\u00a0<code>K<\/code>\u00a0bananas, she eats all of them instead, and won&#8217;t eat any more bananas during this hour.<\/p>\n<p>Koko likes to eat slowly, but still wants to finish eating all the bananas before the guards come back.<\/p>\n<p>Return the minimum integer\u00a0<code>K<\/code>\u00a0such that she can eat all the bananas within\u00a0<code>H<\/code>\u00a0hours.<\/p>\n<p><strong>Example 1:<\/strong><\/p>\n<pre class=\"crayon:false\"><strong>Input: <\/strong>piles = <span id=\"example-input-1-1\">[3,6,7,11]<\/span>, H = <span id=\"example-input-1-2\">8<\/span>\r\n<strong>Output: <\/strong><span id=\"example-output-1\">4<\/span>\r\n<\/pre>\n<p><strong>Example 2:<\/strong><\/p>\n<pre class=\"crayon:false\"><strong>Input: <\/strong>piles = <span id=\"example-input-2-1\">[30,11,23,4,20]<\/span>, H = <span id=\"example-input-2-2\">5<\/span>\r\n<strong>Output: <\/strong><span id=\"example-output-2\">30<\/span>\r\n<\/pre>\n<p><strong>Example 3:<\/strong><\/p>\n<pre class=\"crayon:false\"><strong>Input: <\/strong>piles = <span id=\"example-input-3-1\">[30,11,23,4,20]<\/span>, H = <span id=\"example-input-3-2\">6<\/span>\r\n<strong>Output: <\/strong><span id=\"example-output-3\">23<\/span>\r\n<\/pre>\n<p><strong>Note:<\/strong><\/p>\n<ul>\n<li><code>1 &lt;= piles.length &lt;= 10^4<\/code><\/li>\n<li><code>piles.length &lt;= H &lt;= 10^9<\/code><\/li>\n<li><code>1 &lt;= piles[i] &lt;= 10^9<\/code><\/li>\n<\/ul>\n<h1><strong>Solution: Binary Search<\/strong><\/h1>\n<p>search for the smallest k [1, max_pile_height] such that eating time h &lt;= H.<\/p>\n<p>Time complexity: O(nlogh)<\/p>\n<p>Space complexity: O(1)<\/p>\n<pre class=\"lang:default decode:true \">\/\/ Author: Huahua\r\n\/\/ Running time: 48 ms\r\nclass Solution {\r\npublic:\r\n  int minEatingSpeed(vector&lt;int&gt;&amp; piles, int H) {\r\n    int l = 1;\r\n    int r = *max_element(begin(piles), end(piles)) + 1;\r\n    while (l &lt; r) {\r\n      int m = (r - l) \/ 2 + l;\r\n      int h = 0;\r\n      for (int p : piles)\r\n        h += (p + m - 1) \/ m;\r\n      if (h &lt;= H)\r\n        r = m;\r\n      else\r\n        l = m + 1;\r\n    }\r\n    return l;\r\n  }\r\n};<\/pre>\n<p>&nbsp;<\/p>\n","protected":false},"excerpt":{"rendered":"<p>Problem Koko loves to eat bananas.\u00a0 There are\u00a0N\u00a0piles of bananas, the\u00a0i-th\u00a0pile has\u00a0piles[i]\u00a0bananas.\u00a0 The guards have gone and will come back in\u00a0H\u00a0hours. Koko can decide her&#8230;<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[149],"tags":[52,177],"class_list":["post-3260","post","type-post","status-publish","format-standard","hentry","category-binary-search","tag-binary-search","tag-medium","entry","simple"],"_links":{"self":[{"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/posts\/3260","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/comments?post=3260"}],"version-history":[{"count":7,"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/posts\/3260\/revisions"}],"predecessor-version":[{"id":3267,"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/posts\/3260\/revisions\/3267"}],"wp:attachment":[{"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/media?parent=3260"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/categories?post=3260"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/tags?post=3260"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}