Advertisement

Sum Game - LeetCode 1927 Solution

Sum Game - Complete Solution Guide

Sum Game is LeetCode problem 1927, 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 take turns playing a game, with Alice starting first . You are given a string num of even length consisting of digits and '?' characters. On each turn, a player will do the following if there is still at least one '?' in num : Choose an index i where num[i] == '?' . Replace num[i] with any digit between '0' and '9' . The game ends when there are no more '?' characters in num . For Bob to win, the sum of the digits in the first half of num must be equal to the sum of the digits in t

Detailed Explanation

The problem describes a game between Alice and Bob played with a string `num` of even length, consisting of digits and question marks ('?'). Alice goes first and players take turns replacing a '?' with a digit from '0' to '9'. The game ends when there are no more question marks. Bob wins if the sum of digits in the first half of the string equals the sum of digits in the second half. Alice wins if the sums are unequal. We need to determine if Alice can win, assuming both players play optimally.

Solution Approach

The solution calculates the sum of digits and the number of question marks in each half of the string. If the total number of question marks is odd, Alice wins immediately. If the total is even, we calculate the difference in sums and the difference in the number of question marks between the two halves. Bob wins if the initial difference in the sums can be equalized to zero by filling the question marks appropriately. Specifically, since each '?' that Alice fills can be balanced by Bob in a way that the combined impact of their moves is 9 (e.g., Alice puts 9, Bob puts 0) the difference is handled as `(q_diff / 2) * 9` and should negate the initial `sum_diff`.

Step-by-Step Algorithm

  1. Step 1: Calculate the length of the input string `num` and find the midpoint `half`.
  2. Step 2: Iterate through the first half of the string, calculating the sum of digits (`s1`) and the number of question marks (`q1`).
  3. Step 3: Iterate through the second half of the string, calculating the sum of digits (`s2`) and the number of question marks (`q2`).
  4. Step 4: Calculate the total number of question marks (`total_q = q1 + q2`).
  5. Step 5: If `total_q` is odd, return `true` (Alice wins).
  6. Step 6: Calculate the difference in sums (`sum_diff = s1 - s2`) and the difference in the number of question marks (`q_diff = q1 - q2`).
  7. Step 7: Check if `sum_diff + (q_diff / 2) * 9 == 0`. If it is, return `false` (Bob wins). Otherwise, return `true` (Alice wins).

Key Insights

  • Insight 1: The optimal strategy involves recognizing that if the number of '?' is odd, Alice always wins because she can ensure the two halves are unequal.
  • Insight 2: When the number of '?' is even, the crucial factor is the difference between the sums and the difference between the number of '?' in each half. Bob aims to equalize the sums, while Alice aims to create a difference.
  • Insight 3: For even numbers of '?', Bob will win if the initial difference between the two halves can be offset by the optimal placement of digits in the question marks.

Complexity Analysis

Time Complexity: O(n)

Space Complexity: O(1)

Topics

This problem involves: Math, String, Greedy, Game Theory.

Companies

Asked at: ByteDance, DE Shaw.