{"id":8494,"date":"2021-08-05T22:59:36","date_gmt":"2021-08-06T05:59:36","guid":{"rendered":"https:\/\/zxi.mytechroad.com\/blog\/?p=8494"},"modified":"2021-08-07T14:18:48","modified_gmt":"2021-08-07T21:18:48","slug":"leetcode-1872-stone-game-viii","status":"publish","type":"post","link":"https:\/\/zxi.mytechroad.com\/blog\/dynamic-programming\/leetcode-1872-stone-game-viii\/","title":{"rendered":"\u82b1\u82b1\u9171 LeetCode 1872. Stone Game VIII"},"content":{"rendered":"\n<figure class=\"wp-block-embed is-type-video is-provider-youtube wp-block-embed-youtube wp-embed-aspect-16-9 wp-has-aspect-ratio\"><div class=\"wp-block-embed__wrapper\">\n<iframe loading=\"lazy\" title=\"\u82b1\u82b1\u9171 LeetCode 1872. Stone Game VIII - \u5237\u9898\u627e\u5de5\u4f5c EP394\" width=\"500\" height=\"281\" src=\"https:\/\/www.youtube.com\/embed\/5EaopigkqUc?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 take turns playing a game, with&nbsp;<strong>Alice starting first<\/strong>.<\/p>\n\n\n\n<p>There are&nbsp;<code>n<\/code>&nbsp;stones arranged in a row. On each player&#8217;s turn, while the number of stones is&nbsp;<strong>more than one<\/strong>, they will do the following:<\/p>\n\n\n\n<ol class=\"wp-block-list\"><li>Choose an integer&nbsp;<code>x &gt; 1<\/code>, and&nbsp;<strong>remove<\/strong>&nbsp;the leftmost&nbsp;<code>x<\/code>&nbsp;stones from the row.<\/li><li>Add the&nbsp;<strong>sum<\/strong>&nbsp;of the&nbsp;<strong>removed<\/strong>&nbsp;stones&#8217; values to the player&#8217;s score.<\/li><li>Place a&nbsp;<strong>new stone<\/strong>, whose value is equal to that sum, on the left side of the row.<\/li><\/ol>\n\n\n\n<p>The game stops when&nbsp;<strong>only<\/strong>&nbsp;<strong>one<\/strong>&nbsp;stone is left in the row.<\/p>\n\n\n\n<p>The&nbsp;<strong>score difference<\/strong>&nbsp;between Alice and Bob is&nbsp;<code>(Alice's score - Bob's score)<\/code>. Alice&#8217;s goal is to&nbsp;<strong>maximize<\/strong>&nbsp;the score difference, and Bob&#8217;s goal is the&nbsp;<strong>minimize<\/strong>&nbsp;the score difference.<\/p>\n\n\n\n<p>Given an integer array&nbsp;<code>stones<\/code>&nbsp;of length&nbsp;<code>n<\/code>&nbsp;where&nbsp;<code>stones[i]<\/code>&nbsp;represents the value of the&nbsp;<code>i<sup>th<\/sup><\/code>&nbsp;stone&nbsp;<strong>from the left<\/strong>, return&nbsp;<em>the&nbsp;<strong>score difference<\/strong>&nbsp;between Alice and Bob if they both play&nbsp;<strong>optimally<\/strong>.<\/em><\/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> stones = [-1,2,-3,4,-5]\n<strong>Output:<\/strong> 5\n<strong>Explanation:<\/strong>\n- Alice removes the first 4 stones, adds (-1) + 2 + (-3) + 4 = 2 to her score, and places a stone of\n  value 2 on the left. stones = [2,-5].\n- Bob removes the first 2 stones, adds 2 + (-5) = -3 to his score, and places a stone of value -3 on\n  the left. stones = [-3].\nThe difference between their scores is 2 - (-3) = 5.\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> stones = [7,-6,5,10,5,-2,-6]\n<strong>Output:<\/strong> 13\n<strong>Explanation:<\/strong>\n- Alice removes all stones, adds 7 + (-6) + 5 + 10 + 5 + (-2) + (-6) = 13 to her score, and places a\n  stone of value 13 on the left. stones = [13].\nThe difference between their scores is 13 - 0 = 13.\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> stones = [-10,-12]\n<strong>Output:<\/strong> -22\n<strong>Explanation:<\/strong>\n- Alice can only make one move, which is to remove both stones. She adds (-10) + (-12) = -22 to her\n  score and places a stone of value -22 on the left. stones = [-22].\nThe difference between their scores is (-22) - 0 = -22.\n<\/pre>\n\n\n\n<p><strong>Constraints:<\/strong><\/p>\n\n\n\n<ul class=\"wp-block-list\"><li><code>n == stones.length<\/code><\/li><li><code>2 &lt;= n &lt;= 10<sup>5<\/sup><\/code><\/li><li><code>-10<sup>4<\/sup>&nbsp;&lt;= stones[i] &lt;= 10<sup>4<\/sup><\/code><\/li><\/ul>\n\n\n\n<h2 class=\"wp-block-heading\"><strong>Solution: Prefix Sum + DP<\/strong><\/h2>\n\n\n\n<figure class=\"wp-block-image size-large\"><a href=\"https:\/\/zxi.mytechroad.com\/blog\/wp-content\/uploads\/2021\/08\/1872-ep393-1.png\"><img loading=\"lazy\" decoding=\"async\" width=\"960\" height=\"540\" src=\"https:\/\/zxi.mytechroad.com\/blog\/wp-content\/uploads\/2021\/08\/1872-ep393-1.png\" alt=\"\" class=\"wp-image-8513\" srcset=\"https:\/\/zxi.mytechroad.com\/blog\/wp-content\/uploads\/2021\/08\/1872-ep393-1.png 960w, https:\/\/zxi.mytechroad.com\/blog\/wp-content\/uploads\/2021\/08\/1872-ep393-1-300x169.png 300w, https:\/\/zxi.mytechroad.com\/blog\/wp-content\/uploads\/2021\/08\/1872-ep393-1-768x432.png 768w\" sizes=\"auto, (max-width: 960px) 100vw, 960px\" \/><\/a><\/figure>\n\n\n\n<figure class=\"wp-block-image size-large\"><a href=\"https:\/\/zxi.mytechroad.com\/blog\/wp-content\/uploads\/2021\/08\/1872-ep393-2.png\"><img loading=\"lazy\" decoding=\"async\" width=\"960\" height=\"540\" src=\"https:\/\/zxi.mytechroad.com\/blog\/wp-content\/uploads\/2021\/08\/1872-ep393-2.png\" alt=\"\" class=\"wp-image-8514\" srcset=\"https:\/\/zxi.mytechroad.com\/blog\/wp-content\/uploads\/2021\/08\/1872-ep393-2.png 960w, https:\/\/zxi.mytechroad.com\/blog\/wp-content\/uploads\/2021\/08\/1872-ep393-2-300x169.png 300w, https:\/\/zxi.mytechroad.com\/blog\/wp-content\/uploads\/2021\/08\/1872-ep393-2-768x432.png 768w\" sizes=\"auto, (max-width: 960px) 100vw, 960px\" \/><\/a><\/figure>\n\n\n\n<figure class=\"wp-block-image size-large\"><a href=\"https:\/\/zxi.mytechroad.com\/blog\/wp-content\/uploads\/2021\/08\/1872-ep393-3.png\"><img loading=\"lazy\" decoding=\"async\" width=\"960\" height=\"540\" src=\"https:\/\/zxi.mytechroad.com\/blog\/wp-content\/uploads\/2021\/08\/1872-ep393-3.png\" alt=\"\" class=\"wp-image-8515\" srcset=\"https:\/\/zxi.mytechroad.com\/blog\/wp-content\/uploads\/2021\/08\/1872-ep393-3.png 960w, https:\/\/zxi.mytechroad.com\/blog\/wp-content\/uploads\/2021\/08\/1872-ep393-3-300x169.png 300w, https:\/\/zxi.mytechroad.com\/blog\/wp-content\/uploads\/2021\/08\/1872-ep393-3-768x432.png 768w\" sizes=\"auto, (max-width: 960px) 100vw, 960px\" \/><\/a><\/figure>\n\n\n\n<figure class=\"wp-block-image size-large\"><a href=\"https:\/\/zxi.mytechroad.com\/blog\/wp-content\/uploads\/2021\/08\/1872-ep393-4-1.png\"><img loading=\"lazy\" decoding=\"async\" width=\"960\" height=\"540\" src=\"https:\/\/zxi.mytechroad.com\/blog\/wp-content\/uploads\/2021\/08\/1872-ep393-4-1.png\" alt=\"\" class=\"wp-image-8518\" srcset=\"https:\/\/zxi.mytechroad.com\/blog\/wp-content\/uploads\/2021\/08\/1872-ep393-4-1.png 960w, https:\/\/zxi.mytechroad.com\/blog\/wp-content\/uploads\/2021\/08\/1872-ep393-4-1-300x169.png 300w, https:\/\/zxi.mytechroad.com\/blog\/wp-content\/uploads\/2021\/08\/1872-ep393-4-1-768x432.png 768w\" sizes=\"auto, (max-width: 960px) 100vw, 960px\" \/><\/a><\/figure>\n\n\n\n<p>Note: Naive DP (min-max) takes O(n<sup>2<\/sup>) which leads to TLE. The key of this problem is that each player takes k stones, but put their sum back as a new stone, so you can assume <strong>all the original<\/strong> stones are still there, but opponent has to start from the k+1 th stone.<\/p>\n\n\n\n<p>Let dp[i] denote the max score diff that current player can achieve by taking stones[0~i] (or equivalent)<\/p>\n\n\n\n<p>dp[n-1] = sum(A[0~n-1]) \/\/ Alice takes all the stones.<br>dp[n-2] = sum(A[0~n-2]) &#8211; (A[n-1] + sum(A[0~n-2])) = sum(A[0~n-2]) &#8211; dp[n-1] \/\/ Alice takes n-1 stones, Bob take the last one (A[n-1]) + put-back-stone.<br>dp[n-3] = sum(A[0~n-3]) &#8211; max(dp[n-2], dp[n-1]) \/\/ Alice takes n-2 stones, Bob has two options (takes n-1 stones or takes n stones)<br>&#8230;<br>dp[0] = A[0] &#8211; max(dp[n-1], dp[n-1], &#8230;, dp[1]) \/\/ Alice takes the first stone, Bob has n-1 options.<\/p>\n\n\n\n<p>Time complexity: O(n)<br>Space complexity: O(1)<\/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 stoneGameVIII(vector<int>& stones) {\n    const int n = stones.size();\n    for (int i = 1; i < n; ++i)\n      stones[i] += stones[i - 1];\n    int ans = stones.back(); \/\/ take all the stones.\n    for (int i = n - 2; i > 0; --i)\n      ans = max(ans, stones[i] - ans);\n    return ans;\n  }\n};\n<\/pre>\n<\/div><\/div>\n","protected":false},"excerpt":{"rendered":"<p>Alice and Bob take turns playing a game, with&nbsp;Alice starting first. There are&nbsp;n&nbsp;stones arranged in a row. On each player&#8217;s turn, while the number 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,27,217],"class_list":["post-8494","post","type-post","status-publish","format-standard","hentry","category-dynamic-programming","tag-dp","tag-game","tag-hard","entry","simple"],"_links":{"self":[{"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/posts\/8494","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=8494"}],"version-history":[{"count":5,"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/posts\/8494\/revisions"}],"predecessor-version":[{"id":8519,"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/posts\/8494\/revisions\/8519"}],"wp:attachment":[{"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/media?parent=8494"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/categories?post=8494"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/tags?post=8494"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}