<?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>Game Theory Archives - Huahua&#039;s Tech Road</title>
	<atom:link href="https://zxi.mytechroad.com/blog/category/game-theory/feed/" rel="self" type="application/rss+xml" />
	<link>https://zxi.mytechroad.com/blog/category/game-theory/</link>
	<description></description>
	<lastBuildDate>Fri, 17 Jul 2020 15:51:17 +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>Game Theory Archives - Huahua&#039;s Tech Road</title>
	<link>https://zxi.mytechroad.com/blog/category/game-theory/</link>
	<width>32</width>
	<height>32</height>
</image> 
	<item>
		<title>花花酱 LeetCode 1510. Stone Game IV</title>
		<link>https://zxi.mytechroad.com/blog/game-theory/leetcode-1510-stone-game-iv/</link>
					<comments>https://zxi.mytechroad.com/blog/game-theory/leetcode-1510-stone-game-iv/#respond</comments>
		
		<dc:creator><![CDATA[zxi]]></dc:creator>
		<pubDate>Fri, 17 Jul 2020 15:37:43 +0000</pubDate>
				<category><![CDATA[Game Theory]]></category>
		<category><![CDATA[dp]]></category>
		<category><![CDATA[game]]></category>
		<category><![CDATA[hard]]></category>
		<guid isPermaLink="false">https://zxi.mytechroad.com/blog/?p=7107</guid>

					<description><![CDATA[<p>Alice and Bob take turns playing a game, with Alice starting first. Initially, there are&#160;n&#160;stones in a pile.&#160; On each player&#8217;s turn, that player makes&#8230;</p>
<p>The post <a rel="nofollow" href="https://zxi.mytechroad.com/blog/game-theory/leetcode-1510-stone-game-iv/">花花酱 LeetCode 1510. Stone Game IV</a> appeared first on <a rel="nofollow" href="https://zxi.mytechroad.com/blog">Huahua&#039;s Tech Road</a>.</p>
]]></description>
										<content:encoded><![CDATA[
<p>Alice and Bob take turns playing a game, with Alice starting first.</p>



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



<p>Also, if a player cannot make a move, he/she loses the game.</p>



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



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



<pre class="wp-block-preformatted;crayon:false"><strong>Input:</strong> n = 1
<strong>Output:</strong> true
<strong>Explanation: </strong>Alice can remove 1 stone winning the game because Bob doesn't have any moves.</pre>



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



<pre class="wp-block-preformatted;crayon:false"><strong>Input:</strong> n = 2
<strong>Output:</strong> false
<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>



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



<pre class="wp-block-preformatted;crayon:false"><strong>Input:</strong> n = 4
<strong>Output:</strong> true
<strong>Explanation:</strong> n is already a perfect square, Alice can win with one move, removing 4 stones (4 -&gt; 0).
</pre>



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



<pre class="wp-block-preformatted;crayon:false"><strong>Input:</strong> n = 7
<strong>Output:</strong> false
<strong>Explanation: </strong>Alice can't win the game if Bob plays optimally.
If 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). 
If 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>



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



<pre class="wp-block-preformatted;crayon:false"><strong>Input:</strong> n = 17
<strong>Output:</strong> false
<strong>Explanation: </strong>Alice can't win the game if Bob plays optimally.
</pre>



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



<ul><li><code>1 &lt;= n &lt;= 10^5</code></li></ul>



<h2><strong>Solution: Recursion w/ Memoization / DP</strong></h2>



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



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



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

<pre class="crayon-plain-tag">class Solution {
public:
  bool winnerSquareGame(int n) {    
    vector&lt;int&gt; cache(n + 1, 0); // 0:Unknown, 1:Win, -1:Lose
    function&lt;int(int)&gt; win = [&amp;](int n) -&gt; int {
      if (n == 0) return -1;
      if (cache[n]) return cache[n];
      for (int i = sqrt(n); i &gt;= 1; --i)
        if (win(n - i * i) &lt; 0) return cache[n] = 1;      
      return cache[n] = -1;
    };
    return win(n) &gt; 0;
  }
};</pre>

</div><h2 class="tabtitle">Java</h2>
<div class="tabcontent">

<pre class="crayon-plain-tag">class Solution {
  private int[] cache;
  public boolean winnerSquareGame(int n) {
    this.cache = new int[n + 1];
    return this.win(n) &gt; 0;
  }
  
  private int win(int n) {
    if (n == 0) return -1;
    if (this.cache[n] != 0) return this.cache[n];
    for (int i = (int)Math.sqrt(n); i &gt;= 1; --i)
      if (win(n - i * i) &lt; 0) 
        return this.cache[n] = 1;
    return this.cache[n] = -1;
  }
}</pre>

</div><h2 class="tabtitle">Python3</h2>
<div class="tabcontent">

<pre class="crayon-plain-tag">class Solution:
  def winnerSquareGame(self, n: int) -&gt; bool:
    dp = [None] * (n + 1)
    dp[0] = False
    for i in range(0, n):      
      if dp[i]: continue
      for j in range(1, n + 1):      
        if i + j * j &gt; n: break
        dp[i + j * j] = True
    return dp[n]</pre>
</div></div>
<p>The post <a rel="nofollow" href="https://zxi.mytechroad.com/blog/game-theory/leetcode-1510-stone-game-iv/">花花酱 LeetCode 1510. Stone Game IV</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-1510-stone-game-iv/feed/</wfw:commentRss>
			<slash:comments>0</slash:comments>
		
		
			</item>
		<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>
