{"id":6587,"date":"2020-04-05T11:54:55","date_gmt":"2020-04-05T18:54:55","guid":{"rendered":"https:\/\/zxi.mytechroad.com\/blog\/?p=6587"},"modified":"2020-04-05T16:03:32","modified_gmt":"2020-04-05T23:03:32","slug":"leetcode-1406-stone-game-iii","status":"publish","type":"post","link":"https:\/\/zxi.mytechroad.com\/blog\/game-theory\/leetcode-1406-stone-game-iii\/","title":{"rendered":"\u82b1\u82b1\u9171 LeetCode 1406. Stone Game III"},"content":{"rendered":"\n<figure class=\"wp-block-embed-youtube wp-block-embed is-type-video is-provider-youtube wp-embed-aspect-4-3 wp-has-aspect-ratio\"><div class=\"wp-block-embed__wrapper\">\n<iframe loading=\"lazy\" title=\"\u82b1\u82b1\u9171 LeetCode 1406. Stone Game III - \u5237\u9898\u627e\u5de5\u4f5c EP318\" width=\"500\" height=\"375\" src=\"https:\/\/www.youtube.com\/embed\/uzfsrChj8dM?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>\n<\/div><\/figure>\n\n\n\n<p>Alice and Bob continue their&nbsp;games with piles of stones. There are several stones&nbsp;<strong>arranged in a row<\/strong>, and each stone has an associated&nbsp;value which is an integer given in the array&nbsp;<code>stoneValue<\/code>.<\/p>\n\n\n\n<p>Alice and Bob take turns, with&nbsp;<strong>Alice<\/strong>&nbsp;starting first. On each player&#8217;s turn, that player&nbsp;can take&nbsp;<strong>1, 2 or 3 stones<\/strong>&nbsp;from&nbsp;the&nbsp;<strong>first<\/strong>&nbsp;remaining stones in the row.<\/p>\n\n\n\n<p>The score of each player is the sum of values of the stones taken. The score of each player is&nbsp;<strong>0<\/strong>&nbsp;initially.<\/p>\n\n\n\n<p>The objective of the game is to end with the highest score, and the winner is the player with the highest score and there could be a tie. The game continues until all the stones have been taken.<\/p>\n\n\n\n<p>Assume&nbsp;Alice&nbsp;and Bob&nbsp;<strong>play optimally<\/strong>.<\/p>\n\n\n\n<p>Return&nbsp;<em>&#8220;Alice&#8221;<\/em>&nbsp;if&nbsp;Alice will win,&nbsp;<em>&#8220;Bob&#8221;<\/em>&nbsp;if Bob will win or&nbsp;<em>&#8220;Tie&#8221;<\/em>&nbsp;if they end the game with the same score.<\/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> values = [1,2,3,7]\n<strong>Output:<\/strong> \"Bob\"\n<strong>Explanation:<\/strong> Alice will always lose. Her best move will be to take three piles and the score become 6. Now the score of Bob is 7 and Bob wins.\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> values = [1,2,3,-9]\n<strong>Output:<\/strong> \"Alice\"\n<strong>Explanation:<\/strong> Alice must choose all the three piles at the first move to win and leave Bob with negative score.\nIf Alice chooses one pile her score will be 1 and the next move Bob's score becomes 5. The next move Alice will take the pile with value = -9 and lose.\nIf Alice chooses two piles her score will be 3 and the next move Bob's score becomes 3. The next move Alice will take the pile with value = -9 and also lose.\nRemember that both play optimally so here Alice will choose the scenario that makes her win.\n<\/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> values = [1,2,3,6]\n<strong>Output:<\/strong> \"Tie\"\n<strong>Explanation:<\/strong> Alice cannot win this game. She can end the game in a draw if she decided to choose all the first three piles, otherwise she will lose.\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> values = [1,2,3,-1,-2,-3,7]\n<strong>Output:<\/strong> \"Alice\"\n<\/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> values = [-1,-2,-3]\n<strong>Output:<\/strong> \"Tie\"\n<\/pre>\n\n\n\n<p><strong>Constraints:<\/strong><\/p>\n\n\n\n<ul class=\"wp-block-list\"><li><code>1 &lt;= values.length &lt;= 50000<\/code><\/li><li><code>-1000&nbsp;&lt;= values[i] &lt;= 1000<\/code><\/li><\/ul>\n\n\n\n<h2 class=\"wp-block-heading\"><strong>Solution: DP with memorization<\/strong><\/h2>\n\n\n\n<figure class=\"wp-block-image size-large\"><img loading=\"lazy\" decoding=\"async\" width=\"960\" height=\"540\" src=\"https:\/\/zxi.mytechroad.com\/blog\/wp-content\/uploads\/2020\/04\/1406-ep318.png\" alt=\"\" class=\"wp-image-6592\" srcset=\"https:\/\/zxi.mytechroad.com\/blog\/wp-content\/uploads\/2020\/04\/1406-ep318.png 960w, https:\/\/zxi.mytechroad.com\/blog\/wp-content\/uploads\/2020\/04\/1406-ep318-300x169.png 300w, https:\/\/zxi.mytechroad.com\/blog\/wp-content\/uploads\/2020\/04\/1406-ep318-768x432.png 768w\" sizes=\"auto, (max-width: 960px) 100vw, 960px\" \/><\/figure>\n\n\n\n<p>dp(i) := max relative score the current player can get if start the game from the i-th stone.<\/p>\n\n\n\n<p>dp(i) = max(sum(values[i:i+k]) &#8211; dp(i + k)) 1 &lt;= k &lt;= 3<\/p>\n\n\n\n<p>Time complexity: O(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++\">\n\/\/ Author: Huahua\nclass Solution {\npublic:\n  string stoneGameIII(vector<int>& stoneValue) {\n    const int n = stoneValue.size();\n    vector<int> mem(n, INT_MIN);\n    \n    \/\/ Maximum `relative score` the current player can achieve\n    \/\/ if start from the i-th stone.\n    function<int(int)> dp = [&](int i) {\n      if (i >= n) return 0; \/\/ end of game.\n      if (mem[i] != INT_MIN) return mem[i];      \n      for (int j = 0, s = 0; j < 3 &#038;&#038; i + j < n; ++j) {\n        s += stoneValue[i + j];\n        \/\/ s - dp(.) to get `relative score`.\n        mem[i] = max(mem[i], s - dp(i + j + 1));\n      }      \n      return mem[i];\n    };\n    \n    const int score = dp(0);    \n    return score > 0 ? \"Alice\" : (score == 0 ? \"Tie\" : \"Bob\");\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 stoneGameIII(self, stoneValue: List[int]) -> str:\n    n = len(stoneValue)\n    stoneValue += [0, 0, 0]\n    dp = [-10**9] * n + [0, 0, 0]\n    for i in range(n - 1, -1, -1):\n      for k in (1, 2, 3):\n        dp[i] = max(dp[i], sum(stoneValue[i:i+k]) - dp[i+k])    \n    return \"Alice\" if dp[0] > 0 else \"Bob\" if dp[0] < 0 else \"Tie\"\n<\/pre>\n<\/div><\/div>\n\n\n\n<h2 class=\"wp-block-heading\"><strong>Related Problems<\/strong><\/h2>\n\n\n\n<ul class=\"wp-block-list\"><li><a href=\"https:\/\/zxi.mytechroad.com\/blog\/dynamic-programming\/leetcode-877-stone-game\/\">https:\/\/zxi.mytechroad.com\/blog\/dynamic-programming\/leetcode-877-stone-game\/<\/a><\/li><li><a href=\"https:\/\/zxi.mytechroad.com\/blog\/recursion\/leetcode-1140-stone-game-ii\/\">https:\/\/zxi.mytechroad.com\/blog\/recursion\/leetcode-1140-stone-game-ii\/<\/a><\/li><li><a href=\"https:\/\/zxi.mytechroad.com\/blog\/leetcode\/leetcode-486-predict-the-winner\/\">https:\/\/zxi.mytechroad.com\/blog\/leetcode\/leetcode-486-predict-the-winner\/<\/a><\/li><\/ul>\n","protected":false},"excerpt":{"rendered":"<p>Alice and Bob continue their&nbsp;games with piles of stones. There are several stones&nbsp;arranged in a row, and each stone has an associated&nbsp;value which is an&#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":[27,579,580],"class_list":["post-6587","post","type-post","status-publish","format-standard","hentry","category-game-theory","tag-game","tag-minmax","tag-score","entry","simple"],"_links":{"self":[{"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/posts\/6587","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=6587"}],"version-history":[{"count":4,"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/posts\/6587\/revisions"}],"predecessor-version":[{"id":6593,"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/posts\/6587\/revisions\/6593"}],"wp:attachment":[{"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/media?parent=6587"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/categories?post=6587"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/tags?post=6587"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}