{"id":6465,"date":"2020-03-13T20:22:21","date_gmt":"2020-03-14T03:22:21","guid":{"rendered":"https:\/\/zxi.mytechroad.com\/blog\/?p=6465"},"modified":"2020-03-13T20:31:13","modified_gmt":"2020-03-14T03:31:13","slug":"leetcode-375-guess-number-higher-or-lower-ii","status":"publish","type":"post","link":"https:\/\/zxi.mytechroad.com\/blog\/dynamic-programming\/leetcode-375-guess-number-higher-or-lower-ii\/","title":{"rendered":"\u82b1\u82b1\u9171 LeetCode 375. Guess Number Higher or Lower II"},"content":{"rendered":"\n<p>We are playing the Guess Game. The game is as follows:<\/p>\n\n\n\n<p>I pick a number from&nbsp;<strong>1<\/strong>&nbsp;to&nbsp;<strong>n<\/strong>. You have to guess which number I picked.<\/p>\n\n\n\n<p>Every time you guess wrong, I&#8217;ll tell you whether the number I picked is higher or lower.<\/p>\n\n\n\n<p>However, when you guess a particular number x, and you guess wrong, you pay&nbsp;<strong>$x<\/strong>. You win the game when you guess the number I picked.<\/p>\n\n\n\n<p><strong>Example:<\/strong><\/p>\n\n\n\n<pre class=\"wp-block-preformatted;crayon:false\">n = 10, I pick 8.\n\nFirst round:  You guess 5, I tell you that it's higher. You pay $5.\nSecond round: You guess 7, I tell you that it's higher. You pay $7.\nThird round:  You guess 9, I tell you that it's lower. You pay $9.\n\nGame over. 8 is the number I picked.\n\nYou end up paying $5 + $7 + $9 = $21.\n<\/pre>\n\n\n\n<p>Given a particular&nbsp;<strong>n \u2265 1<\/strong>, find out how much money you need to have to guarantee a&nbsp;<strong>win<\/strong>.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\"><strong>Solution: DP<\/strong><\/h2>\n\n\n\n<p>Use dp[l][r] to denote the min money to win the game if the current guessing range is [l, r], to guarantee a win, we need to try all possible numbers in [l, r]. Let say we guess K, we need to pay K and the game might continue if we were wrong. cost will be K + max(dp(l, K-1), dp(K+1, r)), we need max to cover all possible cases. Among all Ks, we picked the cheapest one.<\/p>\n\n\n\n<p>dp[l][r] = min(k + max(dp[l][k &#8211; 1], dp[k+1][r]), for l &lt;= k &lt;= r.<\/p>\n\n\n\n<p>Time complexity: O(n^3)<br>Space complexity: O(n^2)<\/p>\n\n\n\n<div class=\"responsive-tabs\">\n<h2 class=\"tabtitle\">C++<\/h2>\n<div class=\"tabcontent\">\n\n<pre lang=\"C++\">\n\/\/ Author: Huahua\nclass Solution {\npublic:\n  int getMoneyAmount(int n) {\n    constexpr int kInf = 1e9;\n    vector<vector<int>> cache(n + 2, vector<int>(n + 2, kInf));\n    function<int(int, int)> dp = [&](int l, int r) {\n      int& ans = cache[l][r];\n      if (l >= r) return 0;\n      if (ans != kInf) return ans;\n      for (int x = l; x <= r; ++x)\n        ans = min(ans, x + max(dp(l, x - 1), dp(x + 1, r)));\n      return ans;\n    };\n    return dp(1, n);\n  }\n};\n<\/pre>\n<\/div><\/div>\n\n\n\n<div class=\"responsive-tabs\">\n<h2 class=\"tabtitle\">Python3<\/h2>\n<div class=\"tabcontent\">\n\n<pre lang=\"python\">\n# Author: Huahua\nclass Solution:\n  def getMoneyAmount(self, n: int) -> int:\n    @lru_cache(maxsize=None)\n    def dp(l, r):      \n      return min([k + max(dp(l, k - 1), dp(k + 1, r)) \n                  for k in range(l, r + 1)]) if l < r else 0\n    return dp(1, n)\n<\/pre>\n<\/div><\/div>\n\n\n\n<p><\/p>\n","protected":false},"excerpt":{"rendered":"<p>We are playing the Guess Game. The game is as follows: I pick a number from&nbsp;1&nbsp;to&nbsp;n. You have to guess which number I picked. Every&#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,27,177,398],"class_list":["post-6465","post","type-post","status-publish","format-standard","hentry","category-dynamic-programming","tag-dp","tag-game","tag-medium","tag-on3","entry","simple"],"_links":{"self":[{"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/posts\/6465","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=6465"}],"version-history":[{"count":2,"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/posts\/6465\/revisions"}],"predecessor-version":[{"id":6467,"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/posts\/6465\/revisions\/6467"}],"wp:attachment":[{"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/media?parent=6465"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/categories?post=6465"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/tags?post=6465"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}