{"id":3407,"date":"2018-08-01T23:05:32","date_gmt":"2018-08-02T06:05:32","guid":{"rendered":"https:\/\/zxi.mytechroad.com\/blog\/?p=3407"},"modified":"2018-08-02T03:10:39","modified_gmt":"2018-08-02T10:10:39","slug":"leetcode-808-soup-servings","status":"publish","type":"post","link":"https:\/\/zxi.mytechroad.com\/blog\/dynamic-programming\/leetcode-808-soup-servings\/","title":{"rendered":"\u82b1\u82b1\u9171 LeetCode 808. Soup Servings"},"content":{"rendered":"<h1>Problem<\/h1>\n<p>There are two types of soup: type A and type B. Initially we have\u00a0<code>N<\/code>\u00a0ml of each type of soup. There are four kinds of operations:<\/p>\n<ol>\n<li>Serve\u00a0100 ml of soup A and 0 ml of soup B<\/li>\n<li>Serve\u00a075 ml of soup A and 25\u00a0ml of soup B<\/li>\n<li>Serve 50 ml of soup A and 50 ml of soup B<\/li>\n<li>Serve 25\u00a0ml of soup A and 75\u00a0ml of soup B<\/li>\n<\/ol>\n<p>When we serve some soup, we give it to someone and we no longer have it.\u00a0 Each turn,\u00a0we will choose from the four operations with equal probability 0.25. If the remaining volume of soup is not enough to complete the operation, we will serve\u00a0as much as we can.\u00a0 We stop once we no longer have some quantity of both types of soup.<\/p>\n<p>Note that we do not have the operation where all 100 ml&#8217;s of soup B are used first.<\/p>\n<p>Return the probability that soup A will be empty\u00a0first, plus half the probability that A and B become empty at the same time.<\/p>\n<pre class=\"crayon:false \"><strong>Example:<\/strong>\r\n<strong>Input:<\/strong> N = 50\r\n<strong>Output:<\/strong> 0.625\r\n<strong>Explanation:<\/strong> \r\nIf we choose the first two operations, A will become empty first. For the third operation, A and B will become empty at the same time. For the fourth operation, B will become empty first. So the total probability of A becoming empty first plus half the probability that A and B become empty at the same time, is 0.25 * (1 + 1 + 0.5 + 0) = 0.625.\r\n\r\n<\/pre>\n<p><strong>Notes:<\/strong><\/p>\n<ul>\n<li><code>0 &lt;= N &lt;= 10^9<\/code>.<\/li>\n<li>Answers within\u00a0<code>10^-6<\/code>\u00a0of the true value will be accepted as correct.<\/li>\n<\/ul>\n<h1><strong>Solution 1: Recursion with Memorization<\/strong><\/h1>\n<p>Time complexity: O(N^2) N ~ 5000 \/ 25 = 200<\/p>\n<p>Space complexity: O(N^2)<\/p>\n<p>C++<\/p>\n<pre class=\"lang:default decode:true\">\/\/ Author: Huahua\r\n\/\/ Running time: 4 ms\r\nclass Solution {\r\npublic:\r\n  double soupServings(int N) {\r\n    if (N &gt;= 5000) return 1.0;\r\n    return prob(N, N);\r\n  }\r\nprivate:\r\n  unordered_map&lt;int, unordered_map&lt;int, double&gt;&gt; m_;\r\n  double prob(int A, int B) {\r\n    static int ops[][2] = {{100, 0}, {75, 25}, {50, 50}, {25, 75}};\r\n    if (A &lt;= 0 &amp;&amp; B &lt;= 0) return 0.5;\r\n    if (A &lt;= 0) return 1.0;    \r\n    if (B &lt;= 0) return 0.0;\r\n    if (m_.count(A) &amp;&amp; m_[A].count(B)) return m_[A][B];    \r\n    m_[A][B] = 0.0;\r\n    for (int i = 0; i &lt; 4; ++i)\r\n      m_[A][B] += 0.25 * prob(A - ops[i][0], B - ops[i][1]);\r\n    return m_[A][B];\r\n  }\r\n};<\/pre>\n<p>&nbsp;<\/p>\n","protected":false},"excerpt":{"rendered":"<p>Problem There are two types of soup: type A and type B. Initially we have\u00a0N\u00a0ml of each type of soup. There are four kinds of&#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":[18,31,177,120,17],"class_list":["post-3407","post","type-post","status-publish","format-standard","hentry","category-dynamic-programming","tag-dp","tag-math","tag-medium","tag-probability","tag-recursion","entry","simple"],"_links":{"self":[{"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/posts\/3407","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=3407"}],"version-history":[{"count":5,"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/posts\/3407\/revisions"}],"predecessor-version":[{"id":3412,"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/posts\/3407\/revisions\/3412"}],"wp:attachment":[{"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/media?parent=3407"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/categories?post=3407"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/tags?post=3407"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}