{"id":7107,"date":"2020-07-17T08:37:43","date_gmt":"2020-07-17T15:37:43","guid":{"rendered":"https:\/\/zxi.mytechroad.com\/blog\/?p=7107"},"modified":"2020-07-17T08:51:17","modified_gmt":"2020-07-17T15:51:17","slug":"leetcode-1510-stone-game-iv","status":"publish","type":"post","link":"https:\/\/zxi.mytechroad.com\/blog\/game-theory\/leetcode-1510-stone-game-iv\/","title":{"rendered":"\u82b1\u82b1\u9171 LeetCode 1510. Stone Game IV"},"content":{"rendered":"\n<p>Alice and Bob take turns playing a game, with Alice starting first.<\/p>\n\n\n\n<p>Initially, there are&nbsp;<code>n<\/code>&nbsp;stones in a pile.&nbsp; On each player&#8217;s turn, that player makes a&nbsp;<em>move<\/em>&nbsp;consisting of removing&nbsp;<strong>any<\/strong>&nbsp;non-zero&nbsp;<strong>square number<\/strong>&nbsp;of stones in the pile.<\/p>\n\n\n\n<p>Also, if a player cannot make a move, he\/she loses the game.<\/p>\n\n\n\n<p>Given a positive&nbsp;integer&nbsp;<code>n<\/code>.&nbsp;Return&nbsp;<code>True<\/code>&nbsp;if and only if Alice wins the game otherwise return&nbsp;<code>False<\/code>, assuming both players play optimally.<\/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 = 1\n<strong>Output:<\/strong> true\n<strong>Explanation: <\/strong>Alice can remove 1 stone winning the game because Bob doesn't have any moves.<\/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 = 2\n<strong>Output:<\/strong> false\n<strong>Explanation: <\/strong>Alice can only remove 1 stone, after that Bob removes the last one winning the game (2 -&gt; 1 -&gt; 0).<\/pre>\n\n\n\n<p><strong>Example 3:<\/strong><\/p>\n\n\n\n<pre class=\"wp-block-preformatted;crayon:false\"><strong>Input:<\/strong> n = 4\n<strong>Output:<\/strong> true\n<strong>Explanation:<\/strong> n is already a perfect square, Alice can win with one move, removing 4 stones (4 -&gt; 0).\n<\/pre>\n\n\n\n<p><strong>Example 4:<\/strong><\/p>\n\n\n\n<pre class=\"wp-block-preformatted;crayon:false\"><strong>Input:<\/strong> n = 7\n<strong>Output:<\/strong> false\n<strong>Explanation: <\/strong>Alice can't win the game if Bob plays optimally.\nIf Alice starts removing 4 stones, Bob will remove 1 stone then Alice should remove only 1 stone and finally Bob removes the last one (7 -&gt; 3 -&gt; 2 -&gt; 1 -&gt; 0). \nIf Alice starts removing 1 stone, Bob will remove 4 stones then Alice only can remove 1 stone and finally Bob removes the last one (7 -&gt; 6 -&gt; 2 -&gt; 1 -&gt; 0).<\/pre>\n\n\n\n<p><strong>Example 5:<\/strong><\/p>\n\n\n\n<pre class=\"wp-block-preformatted;crayon:false\"><strong>Input:<\/strong> n = 17\n<strong>Output:<\/strong> false\n<strong>Explanation: <\/strong>Alice can't win the game if Bob plays optimally.\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;= 10^5<\/code><\/li><\/ul>\n\n\n\n<h2 class=\"wp-block-heading\"><strong>Solution: Recursion w\/ Memoization \/ DP<\/strong><\/h2>\n\n\n\n<p>Let win(n) denotes whether the current play will win or not.<br>Try all possible square numbers and see whether the other player will lose or not.<br>win(n) = any(win(n &#8211; i*i) == False) ? True : False<br>base case: win(0) = False<\/p>\n\n\n\n<p>Time complexity: O(nsqrt(n))<br>Space complexity: O(n)<\/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  bool winnerSquareGame(int n) {    \n    vector<int> cache(n + 1, 0); \/\/ 0:Unknown, 1:Win, -1:Lose\n    function<int(int)> win = [&](int n) -> int {\n      if (n == 0) return -1;\n      if (cache[n]) return cache[n];\n      for (int i = sqrt(n); i >= 1; --i)\n        if (win(n - i * i) < 0) return cache[n] = 1;      \n      return cache[n] = -1;\n    };\n    return win(n) > 0;\n  }\n};\n<\/pre>\n\n<\/div><h2 class=\"tabtitle\">Java<\/h2>\n<div class=\"tabcontent\">\n\n<pre lang=\"java\">\nclass Solution {\n  private int[] cache;\n  public boolean winnerSquareGame(int n) {\n    this.cache = new int[n + 1];\n    return this.win(n) > 0;\n  }\n  \n  private int win(int n) {\n    if (n == 0) return -1;\n    if (this.cache[n] != 0) return this.cache[n];\n    for (int i = (int)Math.sqrt(n); i >= 1; --i)\n      if (win(n - i * i) < 0) \n        return this.cache[n] = 1;\n    return this.cache[n] = -1;\n  }\n}\n<\/pre>\n\n<\/div><h2 class=\"tabtitle\">Python3<\/h2>\n<div class=\"tabcontent\">\n\n<pre>\nclass Solution:\n  def winnerSquareGame(self, n: int) -> bool:\n    dp = [None] * (n + 1)\n    dp[0] = False\n    for i in range(0, n):      \n      if dp[i]: continue\n      for j in range(1, n + 1):      \n        if i + j * j > n: break\n        dp[i + j * j] = True\n    return dp[n]\n<\/pre>\n<\/div><\/div>\n","protected":false},"excerpt":{"rendered":"<p>Alice and Bob take turns playing a game, with Alice starting first. Initially, there are&nbsp;n&nbsp;stones in a pile.&nbsp; On each player&#8217;s turn, that player makes&#8230;<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[581],"tags":[18,27,217],"class_list":["post-7107","post","type-post","status-publish","format-standard","hentry","category-game-theory","tag-dp","tag-game","tag-hard","entry","simple"],"_links":{"self":[{"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/posts\/7107","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=7107"}],"version-history":[{"count":3,"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/posts\/7107\/revisions"}],"predecessor-version":[{"id":7110,"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/posts\/7107\/revisions\/7110"}],"wp:attachment":[{"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/media?parent=7107"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/categories?post=7107"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/tags?post=7107"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}