Class Solution

java.lang.Object
g2001_2100.s2029_stone_game_ix.Solution

public class Solution extends Object
2029 - Stone Game IX.<p>Medium</p> <p>Alice and Bob continue their games with stones. There is a row of n stones, and each stone has an associated value. You are given an integer array <code>stones</code>, where <code>stones[i]</code> is the <strong>value</strong> of the <code>i<sup>th</sup></code> stone.</p> <p>Alice and Bob take turns, with <strong>Alice</strong> starting first. On each turn, the player may remove any stone from <code>stones</code>. The player who removes a stone <strong>loses</strong> if the <strong>sum</strong> of the values of <strong>all removed stones</strong> is divisible by <code>3</code>. Bob will win automatically if there are no remaining stones (even if it is Alice&rsquo;s turn).</p> <p>Assuming both players play <strong>optimally</strong> , return <code>true</code> <em>if Alice wins and</em> <code>false</code> <em>if Bob wins</em>.</p> <p><strong>Example 1:</strong></p> <p><strong>Input:</strong> stones = [2,1]</p> <p><strong>Output:</strong> true</p> <p><strong>Explanation:</strong> The game will be played as follows:</p> <ul> <li> <p>Turn 1: Alice can remove either stone.</p> </li> <li> <p>Turn 2: Bob removes the remaining stone.</p> </li> </ul> <p>The sum of the removed stones is 1 + 2 = 3 and is divisible by 3. Therefore, Bob loses and Alice wins the game.</p> <p><strong>Example 2:</strong></p> <p><strong>Input:</strong> stones = [2]</p> <p><strong>Output:</strong> false</p> <p><strong>Explanation:</strong> Alice will remove the only stone, and the sum of the values on the removed stones is 2.</p> <p>Since all the stones are removed and the sum of values is not divisible by 3, Bob wins the game.</p> <p><strong>Example 3:</strong></p> <p><strong>Input:</strong> stones = [5,1,2,4,3]</p> <p><strong>Output:</strong> false</p> <p><strong>Explanation:</strong> Bob will always win. One possible way for Bob to win is shown below:</p> <ul> <li> <p>Turn 1: Alice can remove the second stone with value 1. Sum of removed stones = 1.</p> </li> <li> <p>Turn 2: Bob removes the fifth stone with value 3. Sum of removed stones = 1 + 3 = 4.</p> </li> <li> <p>Turn 3: Alices removes the fourth stone with value 4. Sum of removed stones = 1 + 3 + 4 = 8.</p> </li> <li> <p>Turn 4: Bob removes the third stone with value 2. Sum of removed stones = 1 + 3 + 4 + 2 = 10.</p> </li> <li> <p>Turn 5: Alice removes the first stone with value 5. Sum of removed stones = 1 + 3 + 4 + 2 + 5 = 15.</p> </li> </ul> <p>Alice loses the game because the sum of the removed stones (15) is divisible by 3. Bob wins the game.</p> <p><strong>Constraints:</strong></p> <ul> <li><code>1 <= stones.length <= 10<sup>5</sup></code></li> <li><code>1 <= stones[i] <= 10<sup>4</sup></code></li> </ul>
  • Constructor Details

    • Solution

      public Solution()
  • Method Details

    • stoneGameIX

      public boolean stoneGameIX(int[] stones)