{"id":3525,"date":"2018-08-13T20:42:20","date_gmt":"2018-08-14T03:42:20","guid":{"rendered":"https:\/\/zxi.mytechroad.com\/blog\/?p=3525"},"modified":"2020-08-07T21:26:52","modified_gmt":"2020-08-08T04:26:52","slug":"leetcode-887-super-egg-drop","status":"publish","type":"post","link":"https:\/\/zxi.mytechroad.com\/blog\/dynamic-programming\/leetcode-887-super-egg-drop\/","title":{"rendered":"\u82b1\u82b1\u9171 LeetCode 887. Super Egg Drop"},"content":{"rendered":"\n<figure class=\"wp-block-embed-youtube wp-block-embed is-type-video is-provider-youtube wp-embed-aspect-16-9 wp-has-aspect-ratio\"><div class=\"wp-block-embed__wrapper\">\n<iframe loading=\"lazy\" title=\"\u82b1\u82b1\u9171 LeetCode 887. Super Egg Drop - \u5237\u9898\u627e\u5de5\u4f5c EP 348\" width=\"500\" height=\"281\" src=\"https:\/\/www.youtube.com\/embed\/aPY6sps_Q44?feature=oembed\" frameborder=\"0\" allow=\"accelerometer; autoplay; clipboard-write; encrypted-media; gyroscope; picture-in-picture; web-share\" referrerpolicy=\"strict-origin-when-cross-origin\" allowfullscreen><\/iframe>\n<\/div><\/figure>\n\n\n<h1><strong>Problem<\/strong><\/h1>\n<p>You are given\u00a0<code>K<\/code>\u00a0eggs, and you have access to a building with\u00a0<code>N<\/code>\u00a0floors from\u00a0<code>1<\/code>\u00a0to\u00a0<code>N<\/code>.<\/p>\n<p>Each egg is identical in function, and if an egg breaks, you cannot drop it\u00a0again.<\/p>\n<p>You know that there exists a floor\u00a0<code>F<\/code>\u00a0with\u00a0<code>0 &lt;= F &lt;= N<\/code>\u00a0such that any egg dropped at a floor higher than\u00a0<code>F<\/code>\u00a0will break, and any egg dropped at or below floor\u00a0<code>F<\/code>\u00a0will not break.<\/p>\n<p>Each\u00a0<em>move<\/em>, you may take an egg (if you have an unbroken one) and drop it from any floor\u00a0<code>X<\/code>\u00a0(with\u00a0<code>1 &lt;= X &lt;= N<\/code>).<\/p>\n<p>Your goal is to know\u00a0<strong>with certainty<\/strong>\u00a0what the value of\u00a0<code>F<\/code>\u00a0is.<\/p>\n<p>What is the minimum number of moves that you need to know with certainty\u00a0what\u00a0<code>F<\/code>\u00a0is, regardless of the initial value of\u00a0<code>F<\/code>?<\/p>\n<p><strong>Example 1:<\/strong><\/p>\n<pre class=\"crayon:false\"><strong>Input: <\/strong>K = <span id=\"example-input-1-1\">1<\/span>, N = <span id=\"example-input-1-2\">2<\/span>\n<strong>Output: <\/strong><span id=\"example-output-1\">2<\/span>\n<strong>Explanation: <\/strong>\nDrop the egg from floor 1.  If it breaks, we know with certainty that F = 0.\nOtherwise, drop the egg from floor 2.  If it breaks, we know with certainty that F = 1.\nIf it didn't break, then we know with certainty F = 2.\nHence, we needed 2 moves in the worst case to know what F is with certainty.\n<\/pre>\n<p><strong>Example 2:<\/strong><\/p>\n<pre class=\"crayon:false\"><strong>Input: <\/strong>K = <span id=\"example-input-2-1\">2<\/span>, N = 6\n<strong>Output: <\/strong><span id=\"example-output-2\">3<\/span>\n<\/pre>\n<p><strong>Example 3:<\/strong><\/p>\n<pre class=\"crayon:false\"><strong>Input: <\/strong>K = <span id=\"example-input-3-1\">3<\/span>, N = <span id=\"example-input-3-2\">14<\/span>\n<strong>Output: <\/strong><span id=\"example-output-3\">4<\/span>\n<\/pre>\n<p><strong>Note:<\/strong><\/p>\n<ol>\n<li><code>1 &lt;= K &lt;= 100<\/code><\/li>\n<li><code>1 &lt;= N &lt;= 10000<\/code><\/li>\n<\/ol>\n<h1><strong>Solution 1: Recursion with Memorization (TLE)<\/strong><\/h1>\n<p>\u00a0<\/p>\n<p><img loading=\"lazy\" decoding=\"async\" class=\"alignnone size-large wp-image-7209\" src=\"https:\/\/zxi.mytechroad.com\/blog\/wp-content\/uploads\/2018\/08\/887-ep348-1.png\" alt=\"\" width=\"960\" height=\"540\" srcset=\"https:\/\/zxi.mytechroad.com\/blog\/wp-content\/uploads\/2018\/08\/887-ep348-1.png 960w, https:\/\/zxi.mytechroad.com\/blog\/wp-content\/uploads\/2018\/08\/887-ep348-1-300x169.png 300w, https:\/\/zxi.mytechroad.com\/blog\/wp-content\/uploads\/2018\/08\/887-ep348-1-768x432.png 768w\" sizes=\"auto, (max-width: 960px) 100vw, 960px\" \/><\/p>\n<p><img loading=\"lazy\" decoding=\"async\" class=\"alignnone size-large wp-image-7208\" src=\"https:\/\/zxi.mytechroad.com\/blog\/wp-content\/uploads\/2018\/08\/887-ep348-2.png\" alt=\"\" width=\"960\" height=\"540\" srcset=\"https:\/\/zxi.mytechroad.com\/blog\/wp-content\/uploads\/2018\/08\/887-ep348-2.png 960w, https:\/\/zxi.mytechroad.com\/blog\/wp-content\/uploads\/2018\/08\/887-ep348-2-300x169.png 300w, https:\/\/zxi.mytechroad.com\/blog\/wp-content\/uploads\/2018\/08\/887-ep348-2-768x432.png 768w\" sizes=\"auto, (max-width: 960px) 100vw, 960px\" \/><img loading=\"lazy\" decoding=\"async\" class=\"alignnone size-large wp-image-7207\" src=\"https:\/\/zxi.mytechroad.com\/blog\/wp-content\/uploads\/2018\/08\/887-ep348-3.png\" alt=\"\" width=\"960\" height=\"540\" srcset=\"https:\/\/zxi.mytechroad.com\/blog\/wp-content\/uploads\/2018\/08\/887-ep348-3.png 960w, https:\/\/zxi.mytechroad.com\/blog\/wp-content\/uploads\/2018\/08\/887-ep348-3-300x169.png 300w, https:\/\/zxi.mytechroad.com\/blog\/wp-content\/uploads\/2018\/08\/887-ep348-3-768x432.png 768w\" sizes=\"auto, (max-width: 960px) 100vw, 960px\" \/> \u00a0<\/p>\n<p>dp[k][n] := min number of moves to test n floors with k eggs.<\/p>\n<p>Base cases:<\/p>\n<p>dp[0][n] = 0 # no eggs left.<br \/>dp[1][n] = n\u00a0 # one egg, need to test every floor.<\/p>\n<p>Transition:<\/p>\n<p>dp[k][n] = min(1 + max(dp[k][i &#8211; 1], dp[k &#8211; 1][n &#8211; i])) 1 &lt;= i &lt;= n<\/p>\n<p>Time complexity: O(k*n^2)<\/p>\n<p>Space complexity: O(k*n)<\/p>\n<pre class=\"lang:default decode:true \">\/\/ Author: Huahua\n\/\/ Running time: TLE (69\/121 passed)\nclass Solution {\npublic:\n  int superEggDrop(int K, int N) {    \n    m_ = vector&lt;vector&lt;int&gt;&gt;(K + 1, vector&lt;int&gt;(N + 1, INT_MIN));\n    return dp(K, N);\n  }\nprivate:\n  \/\/ m[i][j] := min moves of i eggs and j floors\n  vector&lt;vector&lt;int&gt;&gt; m_;\n  \n  int dp(int k, int n) {\n    if (k &lt;= 0) return 0;\n    if (k == 1) return n;\n    if (n &lt;= 1) return n;\n    if (m_[k][n] != INT_MIN) return m_[k][n];\n    int ans = INT_MAX;\n    for (int i = 1; i &lt;= n; ++i)\n      ans = min(ans, 1 + max(dp(k - 1, i - 1),   \/\/ broken at floor i, need to test i - 1 floors using k - 1 eggs.\n                             dp(k,     n - i))); \/\/ unbroken at floor i, need to test n - i floors using k eggs. \n    return m_[k][n] = ans;\n  }\n};<\/pre>\n<h1>Solution 2: Solution 1 + Binary Search<\/h1>\n<p><img loading=\"lazy\" decoding=\"async\" class=\"alignnone size-full wp-image-7206\" src=\"https:\/\/zxi.mytechroad.com\/blog\/wp-content\/uploads\/2018\/08\/887-ep348-4.png\" alt=\"\" width=\"960\" height=\"540\" srcset=\"https:\/\/zxi.mytechroad.com\/blog\/wp-content\/uploads\/2018\/08\/887-ep348-4.png 960w, https:\/\/zxi.mytechroad.com\/blog\/wp-content\/uploads\/2018\/08\/887-ep348-4-300x169.png 300w, https:\/\/zxi.mytechroad.com\/blog\/wp-content\/uploads\/2018\/08\/887-ep348-4-768x432.png 768w\" sizes=\"auto, (max-width: 960px) 100vw, 960px\" \/><\/p>\n<p>Time complexity: O(k*n*logn)<\/p>\n<p>Space complexity: O(k*n)<\/p>\n<p>C++<\/p>\n<pre class=\"lang:c++ decode:true  \">\/\/ Author: Huahua\n\/\/ Running time: 40 ms\nclass Solution {\npublic:\n  int superEggDrop(int K, int N) {    \n    m_ = vector&lt;vector&lt;int&gt;&gt;(K + 1, vector&lt;int&gt;(N + 1, INT_MIN));\n    return dp(K, N);\n  }\nprivate:\n  \/\/ m[i][j] := min moves of i eggs and j floors\n  vector&lt;vector&lt;int&gt;&gt; m_;\n  \n  int dp(int k, int n) {\n    if (k &lt;= 0) return 0;\n    if (k == 1) return n;\n    if (n &lt;= 1) return n;\n    if (m_[k][n] != INT_MIN) return m_[k][n];\n        \n    \/\/ broken[i]   = dp(k - 1, i - 1) is incresing with i.\n    \/\/ unbroken[i] = dp(k,     n - i) is decresing with i.\n    \/\/ dp[k][n] = 1 + min(max(broken[i], unbroken[i])), 1 &lt;= i &lt;= n\n    \/\/ find the smallest i such that broken[i] &gt;= unbroken[i],\n    \/\/ which minimizes max(broken[i], unbroken[i]).\n    int l = 1;\n    int r = n + 1;\n    while (l &lt; r) {\n      int m = l + (r - l) \/ 2;\n      int broken = dp(k - 1, m - 1);\n      int unbroken = dp(k, n - m);\n      if (broken &gt;= unbroken)\n        r = m;\n      else\n        l = m + 1;\n    }\n    \n    return m_[k][n] = 1 + dp(k - 1, l - 1);\n  }\n};<\/pre>\n<p>\u00a0<\/p>\n<p>\u00a0<\/p>","protected":false},"excerpt":{"rendered":"<p>Problem You are given\u00a0K\u00a0eggs, and you have access to a building with\u00a0N\u00a0floors from\u00a01\u00a0to\u00a0N. Each egg is identical in function, and if an egg breaks, you&#8230;<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[46],"tags":[52,18],"class_list":["post-3525","post","type-post","status-publish","format-standard","hentry","category-dynamic-programming","tag-binary-search","tag-dp","entry","simple"],"_links":{"self":[{"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/posts\/3525","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=3525"}],"version-history":[{"count":4,"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/posts\/3525\/revisions"}],"predecessor-version":[{"id":7210,"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/posts\/3525\/revisions\/7210"}],"wp:attachment":[{"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/media?parent=3525"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/categories?post=3525"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/tags?post=3525"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}