{"id":1751,"date":"2018-02-04T09:17:44","date_gmt":"2018-02-04T17:17:44","guid":{"rendered":"http:\/\/zxi.mytechroad.com\/blog\/?p=1751"},"modified":"2018-02-04T23:19:01","modified_gmt":"2018-02-05T07:19:01","slug":"leetcode-464-can-i-win","status":"publish","type":"post","link":"https:\/\/zxi.mytechroad.com\/blog\/searching\/leetcode-464-can-i-win\/","title":{"rendered":"\u82b1\u82b1\u9171 LeetCode 464. Can I Win"},"content":{"rendered":"<p><iframe loading=\"lazy\" title=\"\u82b1\u82b1\u9171 LeetCode 464. Can I Win - \u5237\u9898\u627e\u5de5\u4f5c EP165\" width=\"500\" height=\"375\" src=\"https:\/\/www.youtube.com\/embed\/GNZIAbf0gT0?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><\/p>\n<p>\u9898\u76ee\u5927\u610f\uff1a\u4e24\u4e2a\u4eba\u4ece1\u5230M\u4e2d\u6bcf\u6b21\u53d6\u51fa\u4e00\u4e2a\u6570\u52a0\u5230\u5f53\u524d\u7684\u603b\u548c\u4e0a\uff0c\u7b2c\u4e00\u4e2a\u8fbe\u5230\u6216\u8d85\u8fc7T\u7684\u4eba\u83b7\u80dc\u3002\u95ee\u4f60\u7b2c\u4e00\u4e2a\u73a9\u5bb6\u80fd\u4e0d\u80fd\u83b7\u80dc\u3002<\/p>\n<p>In the &#8220;100 game,&#8221; two players take turns adding, to a running total, any integer from 1..10. The player who first causes the running total to reach or exceed 100 wins.<\/p>\n<p>What if we change the game so that players cannot re-use integers?<\/p>\n<p>For example, two players might take turns drawing from a common pool of numbers of 1..15 without replacement until they reach a total &gt;= 100.<\/p>\n<p>Given an integer\u00a0<code>maxChoosableInteger<\/code>\u00a0and another integer\u00a0<code>desiredTotal<\/code>, determine if the first player to move can force a win, assuming both players play optimally.<\/p>\n<p>You can always assume that\u00a0<code>maxChoosableInteger<\/code>\u00a0will not be larger than 20 and\u00a0<code>desiredTotal<\/code>\u00a0will not be larger than 300.<\/p>\n<p><b>Example<\/b><\/p>\n<pre class=\"\">Input:\r\nmaxChoosableInteger = 10\r\ndesiredTotal = 11\r\n\r\nOutput:\r\nfalse\r\n\r\nExplanation:\r\nNo matter which integer the first player choose, the first player will lose.\r\nThe first player can choose an integer from 1 up to 10.\r\nIf the first player choose 1, the second player can only choose integers from 2 up to 10.\r\nThe second player will win by choosing 10 and get a total = 11, which is &gt;= desiredTotal.\r\nSame with other integers chosen by the first player, the second player will always win.<\/pre>\n<p>&nbsp;<\/p>\n<p>&nbsp;<\/p>\n<p><img loading=\"lazy\" decoding=\"async\" class=\"alignnone size-full wp-image-1759\" src=\"http:\/\/zxi.mytechroad.com\/blog\/wp-content\/uploads\/2018\/02\/464-ep165.png\" alt=\"\" width=\"960\" height=\"540\" srcset=\"https:\/\/zxi.mytechroad.com\/blog\/wp-content\/uploads\/2018\/02\/464-ep165.png 960w, https:\/\/zxi.mytechroad.com\/blog\/wp-content\/uploads\/2018\/02\/464-ep165-300x169.png 300w, https:\/\/zxi.mytechroad.com\/blog\/wp-content\/uploads\/2018\/02\/464-ep165-768x432.png 768w\" sizes=\"auto, (max-width: 960px) 100vw, 960px\" \/><\/p>\n<p><img loading=\"lazy\" decoding=\"async\" class=\"alignnone size-full wp-image-1758\" src=\"http:\/\/zxi.mytechroad.com\/blog\/wp-content\/uploads\/2018\/02\/464-ep165-2.png\" alt=\"\" width=\"960\" height=\"540\" srcset=\"https:\/\/zxi.mytechroad.com\/blog\/wp-content\/uploads\/2018\/02\/464-ep165-2.png 960w, https:\/\/zxi.mytechroad.com\/blog\/wp-content\/uploads\/2018\/02\/464-ep165-2-300x169.png 300w, https:\/\/zxi.mytechroad.com\/blog\/wp-content\/uploads\/2018\/02\/464-ep165-2-768x432.png 768w\" sizes=\"auto, (max-width: 960px) 100vw, 960px\" \/><\/p>\n<p><b>Solution: Recursion with memoization\u00a0<\/b><\/p>\n<p>Time complexity: O(2^M)<\/p>\n<p>Space complexity: O(2^M)<\/p>\n<p>C++<\/p>\n<pre class=\"lang:c++ decode:true \">\/\/ Author: Huahua\r\n\/\/ Running time: 19 ms (&lt;98.70%)\r\nclass Solution {\r\npublic:\r\n  bool canIWin(int M, int T) {\r\n    const int sum = M * (M + 1) \/ 2;\r\n    if (sum &lt; T) return false;\r\n    if (T &lt;= 0) return true;\r\n    m_ = vector&lt;char&gt;(1 &lt;&lt; M, 0);\r\n    return canIWin(M, T, 0);\r\n  }\r\nprivate:\r\n  vector&lt;char&gt; m_; \/\/ 0: unknown, 1: won, -1: lost\r\n  bool canIWin(int M, int T, int state) {\r\n    if (T &lt;= 0) return false;\r\n    if (m_[state]) return m_[state] == 1;\r\n    for (int i = 0; i &lt; M; ++i) {\r\n      if (state &amp; (1 &lt;&lt; i)) continue; \/\/ number i used      \r\n      \/\/ The next player can not win, current player wins\r\n      if (!canIWin(M, T - (i + 1), state | (1 &lt;&lt; i))) \r\n        return m_[state] = 1;\r\n    }\r\n    \/\/ Current player loses.\r\n    m_[state] = -1;\r\n    return false;\r\n  }\r\n};<\/pre>\n<p>Java<\/p>\n<pre class=\"lang:java decode:true \">\/\/ Author: Huahua\r\n\/\/ Running time: 20 ms\r\nclass Solution {\r\n  private byte[] m_;\r\n  public boolean canIWin(int M, int T) {\r\n    int sum = M * (M + 1) \/ 2;\r\n    if (sum &lt; T) return false;\r\n    if (T &lt;= 0) return true;\r\n    m_ = new byte[1 &lt;&lt; M];\r\n    return canIWin(M, T, 0);\r\n  }\r\n  \r\n  private boolean canIWin(int M, int T, int state) {\r\n    if (T &lt;= 0) return false;\r\n    if (m_[state] != 0) return m_[state] == 1;\r\n    \r\n    for (int i = 0; i &lt; M; ++i) {\r\n      if ((state &amp; (1 &lt;&lt; i)) &gt; 0) continue;\r\n      if (!canIWin(M, T - (i + 1), state | (1 &lt;&lt; i))) {\r\n        m_[state] = 1;\r\n        return true;\r\n      }\r\n    }\r\n    m_[state] = -1;\r\n    return false;\r\n  }    \r\n}<\/pre>\n<p>Python3<\/p>\n<pre class=\"lang:python decode:true \">\"\"\"\r\nAuthor: Huahua\r\nRunning time: 706 ms\r\n\"\"\"\r\nclass Solution:\r\n  def canIWin(self, M, T):\r\n    def win(M, T, m, state):\r\n      if T &lt;= 0: return False\r\n      if m[state] != 0: return m[state] == 1\r\n      for i in range(M):\r\n        if (state &amp; (1 &lt;&lt; i)) &gt; 0: continue\r\n        if not win(M, T - i - 1, m, state | (1 &lt;&lt; i)):\r\n          m[state] = 1\r\n          return True\r\n      m[state] = -1\r\n      return False\r\n    \r\n    s = M * (M + 1) \/ 2\r\n    if s &lt; T: return False\r\n    if T &lt;= 0: return True\r\n    if s == T: return (M % 2) == 1\r\n    \r\n    m = [0] * (1 &lt;&lt; M)\r\n    return win(M, T, m, 0)<\/pre>\n<p>&nbsp;<\/p>\n","protected":false},"excerpt":{"rendered":"<p>\u9898\u76ee\u5927\u610f\uff1a\u4e24\u4e2a\u4eba\u4ece1\u5230M\u4e2d\u6bcf\u6b21\u53d6\u51fa\u4e00\u4e2a\u6570\u52a0\u5230\u5f53\u524d\u7684\u603b\u548c\u4e0a\uff0c\u7b2c\u4e00\u4e2a\u8fbe\u5230\u6216\u8d85\u8fc7T\u7684\u4eba\u83b7\u80dc\u3002\u95ee\u4f60\u7b2c\u4e00\u4e2a\u73a9\u5bb6\u80fd\u4e0d\u80fd\u83b7\u80dc\u3002 In the &#8220;100 game,&#8221; two players take turns adding, to a running total, any integer from 1..10. The player who first causes the running&#8230;<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[153,44],"tags":[217,25],"class_list":["post-1751","post","type-post","status-publish","format-standard","hentry","category-recursion","category-searching","tag-hard","tag-min-max","entry","simple"],"_links":{"self":[{"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/posts\/1751","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=1751"}],"version-history":[{"count":7,"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/posts\/1751\/revisions"}],"predecessor-version":[{"id":1760,"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/posts\/1751\/revisions\/1760"}],"wp:attachment":[{"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/media?parent=1751"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/categories?post=1751"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/tags?post=1751"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}