Advertisement

Stone Game IX - LeetCode 2029 Solution

Stone Game IX - Complete Solution Guide

Stone Game IX is LeetCode problem 2029, a Medium level challenge. This complete guide provides step-by-step explanations, multiple solution approaches, and optimized code in python3, java, cpp, c.

Problem Statement

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 stones , where stones[i] is the value of the i th stone. Alice and Bob take turns, with Alice starting first. On each turn, the player may remove any stone from stones . The player who removes a stone loses if the sum of the values of all removed stones is divisible by 3 . Bob will win automatically if there are no remaining stones (even if it is Alic

Detailed Explanation

The problem presents a game between Alice and Bob involving a row of stones, each with a value. They take turns removing stones, and the player who makes the cumulative sum of removed stone values divisible by 3 loses. Alice goes first, and Bob wins if there are no stones left. The task is to determine, assuming optimal play by both, whether Alice wins (returns `true`) or Bob wins (returns `false`).

Solution Approach

The provided solutions efficiently determine the winner by counting the number of stones with remainders 0, 1, and 2 when divided by 3. Then, based on whether the count of stones with remainder 0 is even or odd, different winning conditions are evaluated. If the count of remainder 0 is even, Alice wins if there are both remainder 1 and 2 stones available. If the count of remainder 0 is odd, Alice wins if the absolute difference between the counts of stones with remainder 1 and 2 is greater than 2.

Step-by-Step Algorithm

  1. Step 1: Count the number of stones that have remainders 0, 1, and 2 when divided by 3. This can be efficiently done by iterating through the `stones` array and using a HashMap/Counter to track the counts.
  2. Step 2: Check if the count of stones with remainder 0 is even or odd.
  3. Step 3: If the count of remainder 0 stones is even, Alice wins if there's at least one stone with remainder 1 and at least one stone with remainder 2. Return `true` if this condition is met, otherwise return `false`.
  4. Step 4: If the count of remainder 0 stones is odd, Alice wins if the absolute difference between the count of stones with remainder 1 and the count of stones with remainder 2 is greater than 2. Return `true` if this condition is met, otherwise return `false`.

Key Insights

  • Insight 1: The core of the solution lies in understanding how the remainders of stone values when divided by 3 affect the game's outcome. Only the counts of stones with remainders 0, 1, and 2 matter.
  • Insight 2: The number of stones with a remainder of 0 significantly impacts the game dynamic. An even count allows direct competition between stones with remainders 1 and 2, while an odd count introduces a slight advantage/disadvantage depending on the imbalance between counts of remainders 1 and 2.
  • Insight 3: Optimal play involves understanding how to force a win or avoid a loss based on the current state of the counts and the total sum's remainder when divided by 3.

Complexity Analysis

Time Complexity: O(n)

Space Complexity: O(1)

Topics

This problem involves: Array, Math, Greedy, Counting, Game Theory.

Companies

Asked at: Samsung.