Similar Problems

Similar Problems not available

Sum Game - Leetcode Solution

Companies:

  • bytedance

LeetCode:  Sum Game Leetcode Solution

Difficulty: Medium

Topics: greedy string math  

The Sum Game problem on LeetCode asks us to find if Alice can win a game against Bob with certain conditions. The game has two piles of stones, each pile has n stones and each stone has a weight represented by an array of integers. Alice and Bob take turns to remove a stone from either pile. The player who removes the stone with the smaller weight loses one point, and the player who removes the stone with the larger weight gains one point. The game continues until there are no more stones. Alice wants to know if she can win the game no matter how Bob plays.

To solve this problem, we need to find the difference between the sum of the weights of the stones in pile A and the sum of the weights of the stones in pile B. If this difference is not equal to zero, then Alice can win the game. If the difference is equal to zero, then Alice cannot win the game.

Let's say the sum of weights of stones in pile A is Sa and sum of weights of stones in pile B is Sb. Then we need to check if the following two conditions are true:

  1. Sa != Sb
  2. Alice can choose a stone from either pile such that the difference in the sums, after removing that stone from the pile, is not equal to (Sa - Sb) / 2.

If both of these conditions are true, then Alice can win the game no matter how Bob plays. Otherwise, Alice cannot win the game.

To check the second condition, we can use prefix sums. We can calculate the prefix sums of the weights in each pile. Let the prefix sums of the weights in pile A be P and prefix sums of the weights in pile B be Q. Then Alice can choose a stone from pile A such that the difference in the sums after removing that stone is not equal to (Sa - Sb) / 2 if and only if there is an index i such that (P[i] - Q[i]) != ((Sa - Sb) / 2).

Similarly, Alice can choose a stone from pile B such that the difference in the sums after removing that stone is not equal to (Sa - Sb) / 2 if and only if there is an index i such that (Q[i] - P[i]) != ((Sa - Sb) / 2).

We can combine these two conditions and check if Alice can win the game as follows:

  1. Calculate the sum of weights of stones in pile A and the sum of weights of stones in pile B
  2. If Sa = Sb, return False
  3. Calculate the prefix sums of the weights in pile A and in pile B
  4. For each index i, check if (P[i] - Q[i]) = ((Sa - Sb) / 2) or (Q[i] - P[i]) = ((Sa - Sb) / 2). If yes, continue to the next index. If no, return True.

Here is the Python code to implement the above algorithm:

def sumGame(nums: List[int]) -> bool:
    n = len(nums)
    Sa = sum(nums[:n//2])
    Sb = sum(nums[n//2:])
    if Sa == Sb:
        return False
    P = [0] * (n//2 + 1)
    Q = [0] * (n//2 + 1)
    for i in range(n//2):
        P[i+1] = P[i] + nums[i]
        Q[i+1] = Q[i] + nums[n//2+i]
    for i in range(n//2 + 1):
        if abs(P[i] - Q[i]) == abs(Sa - Sb) // 2:
            continue
        else:
            return True
    return False

The time complexity of the above algorithm is O(n) and the space complexity is O(n).

Sum Game Solution Code

1