{"id":8533,"date":"2021-08-08T20:04:58","date_gmt":"2021-08-09T03:04:58","guid":{"rendered":"https:\/\/zxi.mytechroad.com\/blog\/?p=8533"},"modified":"2021-08-08T20:05:18","modified_gmt":"2021-08-09T03:05:18","slug":"leetcode-1884-egg-drop-with-2-eggs-and-n-floors","status":"publish","type":"post","link":"https:\/\/zxi.mytechroad.com\/blog\/math\/leetcode-1884-egg-drop-with-2-eggs-and-n-floors\/","title":{"rendered":"\u82b1\u82b1\u9171 LeetCode 1884. Egg Drop With 2 Eggs and N Floors"},"content":{"rendered":"\n<p>You are given&nbsp;<strong>two identical<\/strong>&nbsp;eggs and you have access to a building with&nbsp;<code>n<\/code>&nbsp;floors labeled from&nbsp;<code>1<\/code>&nbsp;to&nbsp;<code>n<\/code>.<\/p>\n\n\n\n<p>You know that there exists a floor&nbsp;<code>f<\/code>&nbsp;where&nbsp;<code>0 &lt;= f &lt;= n<\/code>&nbsp;such that any egg dropped at a floor&nbsp;<strong>higher<\/strong>&nbsp;than&nbsp;<code>f<\/code>&nbsp;will&nbsp;<strong>break<\/strong>, and any egg dropped&nbsp;<strong>at or below<\/strong>&nbsp;floor&nbsp;<code>f<\/code>&nbsp;will&nbsp;<strong>not break<\/strong>.<\/p>\n\n\n\n<p>In each move, you may take an&nbsp;<strong>unbroken<\/strong>&nbsp;egg and drop it from any floor&nbsp;<code>x<\/code>&nbsp;(where&nbsp;<code>1 &lt;= x &lt;= n<\/code>). If the egg breaks, you can no longer use it. However, if the egg does not break, you may&nbsp;<strong>reuse<\/strong>&nbsp;it in future moves.<\/p>\n\n\n\n<p>Return&nbsp;<em>the&nbsp;<strong>minimum number of moves<\/strong>&nbsp;that you need to determine&nbsp;<strong>with certainty<\/strong>&nbsp;what the value of&nbsp;<\/em><code>f<\/code>&nbsp;is.<\/p>\n\n\n\n<p><strong>Example 1:<\/strong><\/p>\n\n\n\n<pre class=\"wp-block-preformatted;crayon:false;\"><strong>Input:<\/strong> n = 2\n<strong>Output:<\/strong> 2\n<strong>Explanation:<\/strong> We can drop the first egg from floor 1 and the second egg from floor 2.\nIf the first egg breaks, we know that f = 0.\nIf the second egg breaks but the first egg didn't, we know that f = 1.\nOtherwise, if both eggs survive, we know that f = 2.\n<\/pre>\n\n\n\n<p><strong>Example 2:<\/strong><\/p>\n\n\n\n<pre class=\"wp-block-preformatted;crayon:false;\"><strong>Input:<\/strong> n = 100\n<strong>Output:<\/strong> 14\n<strong>Explanation:<\/strong> One optimal strategy is:\n- Drop the 1st egg at floor 9. If it breaks, we know f is between 0 and 8. Drop the 2nd egg starting\n  from floor 1 and going up one at a time to find f within 7 more drops. Total drops is 1 + 7 = 8.\n- If the 1st egg does not break, drop the 1st egg again at floor 22. If it breaks, we know f is between 9\n  and 21. Drop the 2nd egg starting from floor 10 and going up one at a time to find f within 12 more\n  drops. Total drops is 2 + 12 = 14.\n- If the 1st egg does not break again, follow a similar process dropping the 1st egg from floors 34, 45,\n  55, 64, 72, 79, 85, 90, 94, 97, 99, and 100.\nRegardless of the outcome, it takes at most 14 drops to determine f.\n<\/pre>\n\n\n\n<p><strong>Constraints:<\/strong><\/p>\n\n\n\n<ul class=\"wp-block-list\"><li><code>1 &lt;= n &lt;= 1000<\/code><\/li><\/ul>\n\n\n\n<h2 class=\"wp-block-heading\"><strong>Solution: Math<\/strong><\/h2>\n\n\n\n<p>Time complexity: O(1)<br>Space complexity: O(1)<\/p>\n\n\n\n<div class=\"responsive-tabs\">\n<h2 class=\"tabtitle\">C++<\/h2>\n<div class=\"tabcontent\">\n\n<pre lang=\"c++\">\nclass Solution {\npublic:\n  int twoEggDrop(int n) {\n    int ans{0};\n    while (ans < n) n -= ans++;\n    return ans;\n  }\n};\n<\/pre>\n<\/div><\/div>\n","protected":false},"excerpt":{"rendered":"<p>You are given&nbsp;two identical&nbsp;eggs and you have access to a building with&nbsp;n&nbsp;floors labeled from&nbsp;1&nbsp;to&nbsp;n. You know that there exists a floor&nbsp;f&nbsp;where&nbsp;0 &lt;= f &lt;= n&nbsp;such&#8230;<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[49],"tags":[31,177],"class_list":["post-8533","post","type-post","status-publish","format-standard","hentry","category-math","tag-math","tag-medium","entry","simple"],"_links":{"self":[{"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/posts\/8533","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=8533"}],"version-history":[{"count":2,"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/posts\/8533\/revisions"}],"predecessor-version":[{"id":8536,"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/posts\/8533\/revisions\/8536"}],"wp:attachment":[{"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/media?parent=8533"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/categories?post=8533"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/tags?post=8533"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}