<?xml version="1.0" encoding="UTF-8"?><rss version="2.0"
	xmlns:content="http://purl.org/rss/1.0/modules/content/"
	xmlns:wfw="http://wellformedweb.org/CommentAPI/"
	xmlns:dc="http://purl.org/dc/elements/1.1/"
	xmlns:atom="http://www.w3.org/2005/Atom"
	xmlns:sy="http://purl.org/rss/1.0/modules/syndication/"
	xmlns:slash="http://purl.org/rss/1.0/modules/slash/"
	>

<channel>
	<title>score Archives - Huahua&#039;s Tech Road</title>
	<atom:link href="https://zxi.mytechroad.com/blog/tag/score/feed/" rel="self" type="application/rss+xml" />
	<link>https://zxi.mytechroad.com/blog/tag/score/</link>
	<description></description>
	<lastBuildDate>Sun, 05 Apr 2020 23:03:32 +0000</lastBuildDate>
	<language>en</language>
	<sy:updatePeriod>
	hourly	</sy:updatePeriod>
	<sy:updateFrequency>
	1	</sy:updateFrequency>
	<generator>https://wordpress.org/?v=6.0.8</generator>

<image>
	<url>https://zxi.mytechroad.com/blog/wp-content/uploads/2017/09/cropped-photo-32x32.jpg</url>
	<title>score Archives - Huahua&#039;s Tech Road</title>
	<link>https://zxi.mytechroad.com/blog/tag/score/</link>
	<width>32</width>
	<height>32</height>
</image> 
	<item>
		<title>花花酱 LeetCode 1406. Stone Game III</title>
		<link>https://zxi.mytechroad.com/blog/game-theory/leetcode-1406-stone-game-iii/</link>
					<comments>https://zxi.mytechroad.com/blog/game-theory/leetcode-1406-stone-game-iii/#respond</comments>
		
		<dc:creator><![CDATA[zxi]]></dc:creator>
		<pubDate>Sun, 05 Apr 2020 18:54:55 +0000</pubDate>
				<category><![CDATA[Game Theory]]></category>
		<category><![CDATA[game]]></category>
		<category><![CDATA[minmax]]></category>
		<category><![CDATA[score]]></category>
		<guid isPermaLink="false">https://zxi.mytechroad.com/blog/?p=6587</guid>

					<description><![CDATA[<p>Alice and Bob continue their&#160;games with piles of stones. There are several stones&#160;arranged in a row, and each stone has an associated&#160;value which is an&#8230;</p>
<p>The post <a rel="nofollow" href="https://zxi.mytechroad.com/blog/game-theory/leetcode-1406-stone-game-iii/">花花酱 LeetCode 1406. Stone Game III</a> appeared first on <a rel="nofollow" href="https://zxi.mytechroad.com/blog">Huahua&#039;s Tech Road</a>.</p>
]]></description>
										<content:encoded><![CDATA[
<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">
<iframe title="花花酱 LeetCode 1406. Stone Game III - 刷题找工作 EP318" width="500" height="375" src="https://www.youtube.com/embed/uzfsrChj8dM?feature=oembed" frameborder="0" allow="accelerometer; autoplay; encrypted-media; gyroscope; picture-in-picture" allowfullscreen></iframe>
</div></figure>



<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>



<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>



<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>



<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>



<p>Assume&nbsp;Alice&nbsp;and Bob&nbsp;<strong>play optimally</strong>.</p>



<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>



<p><strong>Example 1:</strong></p>



<pre class="wp-block-preformatted;crayon:false"><strong>Input:</strong> values = [1,2,3,7]
<strong>Output:</strong> "Bob"
<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.
</pre>



<p><strong>Example 2:</strong></p>



<pre class="wp-block-preformatted;crayon:false"><strong>Input:</strong> values = [1,2,3,-9]
<strong>Output:</strong> "Alice"
<strong>Explanation:</strong> Alice must choose all the three piles at the first move to win and leave Bob with negative score.
If 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.
If 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.
Remember that both play optimally so here Alice will choose the scenario that makes her win.
</pre>



<p><strong>Example 3:</strong></p>



<pre class="wp-block-preformatted;crayon:false"><strong>Input:</strong> values = [1,2,3,6]
<strong>Output:</strong> "Tie"
<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.
</pre>



<p><strong>Example 4:</strong></p>



<pre class="wp-block-preformatted;crayon:false"><strong>Input:</strong> values = [1,2,3,-1,-2,-3,7]
<strong>Output:</strong> "Alice"
</pre>



<p><strong>Example 5:</strong></p>



<pre class="wp-block-preformatted;crayon:false"><strong>Input:</strong> values = [-1,-2,-3]
<strong>Output:</strong> "Tie"
</pre>



<p><strong>Constraints:</strong></p>



<ul><li><code>1 &lt;= values.length &lt;= 50000</code></li><li><code>-1000&nbsp;&lt;= values[i] &lt;= 1000</code></li></ul>



<h2><strong>Solution: DP with memorization</strong></h2>



<figure class="wp-block-image size-large"><img 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="(max-width: 960px) 100vw, 960px" /></figure>



<p>dp(i) := max relative score the current player can get if start the game from the i-th stone.</p>



<p>dp(i) = max(sum(values[i:i+k]) &#8211; dp(i + k)) 1 &lt;= k &lt;= 3</p>



<p>Time complexity: O(n)<br>Space complexity: O(n)</p>



<div class="responsive-tabs">
<h2 class="tabtitle">C++</h2>
<div class="tabcontent">

<pre class="crayon-plain-tag">// Author: Huahua
class Solution {
public:
  string stoneGameIII(vector&lt;int&gt;&amp; stoneValue) {
    const int n = stoneValue.size();
    vector&lt;int&gt; mem(n, INT_MIN);
    
    // Maximum `relative score` the current player can achieve
    // if start from the i-th stone.
    function&lt;int(int)&gt; dp = [&amp;](int i) {
      if (i &gt;= n) return 0; // end of game.
      if (mem[i] != INT_MIN) return mem[i];      
      for (int j = 0, s = 0; j &lt; 3 &amp;&amp; i + j &lt; n; ++j) {
        s += stoneValue[i + j];
        // s - dp(.) to get `relative score`.
        mem[i] = max(mem[i], s - dp(i + j + 1));
      }      
      return mem[i];
    };
    
    const int score = dp(0);    
    return score &gt; 0 ? &quot;Alice&quot; : (score == 0 ? &quot;Tie&quot; : &quot;Bob&quot;);
  }
};</pre>
</div></div>



<div class="responsive-tabs">
<h2 class="tabtitle">Python3</h2>
<div class="tabcontent">

<pre class="crayon-plain-tag"># Author: Huahua
class Solution:
  def stoneGameIII(self, stoneValue: List[int]) -&gt; str:
    n = len(stoneValue)
    stoneValue += [0, 0, 0]
    dp = [-10**9] * n + [0, 0, 0]
    for i in range(n - 1, -1, -1):
      for k in (1, 2, 3):
        dp[i] = max(dp[i], sum(stoneValue[i:i+k]) - dp[i+k])    
    return &quot;Alice&quot; if dp[0] &gt; 0 else &quot;Bob&quot; if dp[0] &lt; 0 else &quot;Tie&quot;</pre>
</div></div>



<h2><strong>Related Problems</strong></h2>



<ul><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>
<p>The post <a rel="nofollow" href="https://zxi.mytechroad.com/blog/game-theory/leetcode-1406-stone-game-iii/">花花酱 LeetCode 1406. Stone Game III</a> appeared first on <a rel="nofollow" href="https://zxi.mytechroad.com/blog">Huahua&#039;s Tech Road</a>.</p>
]]></content:encoded>
					
					<wfw:commentRss>https://zxi.mytechroad.com/blog/game-theory/leetcode-1406-stone-game-iii/feed/</wfw:commentRss>
			<slash:comments>0</slash:comments>
		
		
			</item>
	</channel>
</rss>
