Tuesday, December 13, 2022

[LeetCode] Stone Game III

Problem: Alice and Bob continue their games with piles of stones. There are several stones arranged in a row, and each stone has an associated value which is an integer given in the array stoneValue.

Alice and Bob take turns, with Alice starting first. On each player's turn, that player can take 1, 2, or 3 stones from the first remaining stones in the row.

The score of each player is the sum of the values of the stones taken. The score of each player is 0 initially.

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.

Assume Alice and Bob play optimally.

Return "Alice" if Alice will win, "Bob" if Bob will win, or "Tie" if they will end the game with the same score.

Example:

Input: values = [1,2,3,7]
Output: "Bob"
Explanation: 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.
Input: values = [1,2,3,-9]
Output: "Alice"
Explanation: 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. In 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. In 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.
Input: values = [1,2,3,6]
Output: "Tie"
Explanation: 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.


Approach: Every time Alice make a move, Bob is going to make his move in such a manner that Alice get the least points from the remaining.

For example- if Alice pick stone[i] and stone[i+1], Alice can pick stone[i +2] or stone[i + 2] + stone[i + 3] or stone[i + 2] + stone[i + 3] + stone[i + 4]. His strategy of picking one of the three combinations would be such that Alice will be able to make the minimum score out of the remaining stones. 

Let's maintain a table of size n where table[i] will tell the max score of Alice for stones[i...n-1]. We can fill the table in following manner:

  • table[n - 1] = stones[n- 1]
  • table[n - 2] = MAX(stones[n - 1], stones[n - 2] + stones[n -1])
  • table[n - 3] = MAX(MIN(stones[n - 3], stones[n - 3] + stones[n - 1]), stones[n - 3] + stones[n - 2], stones[n - 3] + stones[n - 2] + stones[n -1]) // We are choosing Min of stones[n - 3] and  stones[n - 3] + stones[n - 1] because in case of only 3 stones, if Alice chooses 1st stone, Bob can just choose 2nd stone then Alice has to take 3rd stone too. Example: [-1,-2,-3]

For rest of the i: n - 4 to 0

table[i] = MAX of the following

  1. stones[i] + MIN(table[i + 2], table[i + 3], table[i + 4]), // Case when Alice just chooses ith stone, Bob has to choose at least stones[i + 1] so table[i + 1] is not in the picture
  2. stones[i] + stones[i + 1] + MIN(table[i + 3], table[i + 4], table[i + 5]), // Case when Alice chooses ith and (i + 1)th stones
  3. stones[i] + stones[i + 1]  stones[i + 2] + MIN(table[i + 4], table[i + 5], table[i + 6]) // Case when Alice chooses  ith, (i + 1)th (i + 2)th stones

In the end we can check if Alice's max score (table[0]) is more than the half of the sum of all stone values. If yes then Alice will win otherwise Bob will win.

 

Implementation in C#:

    public string StoneGameIII(int[] stoneValue)
    {
int length = stoneValue.Length;

int sum = stoneValue.Sum();
int[] table = new int[length + 3];
for (int i = length - 1; i >= 0; --i)
{
if (i < length - 3)
{
table[i] = this.Max(stoneValue[i] +
                                    this.Min(
    table[i + 2],
table[i + 3],
table[i + 4]),
stoneValue[i] + stoneValue[i + 1] +
                                    this.Min(
    table[i + 3],
table[i + 4],
table[i + 5]),
stoneValue[i] +
                                    stoneValue[i + 1] +
                                    stoneValue[i + 2] +
                                    this.Min(
    table[i + 4],
table[i + 5],
table[i + 6]));
}
else if (i == length - 3)
{
int valAtChoosingFirst = this.Min(stoneValue[i],
                                            stoneValue[i] + stoneValue[i + 2]);
table[length - 3] = this.Max(valAtChoosingFirst,
stoneValue[i] + stoneValue[i + 1],
stoneValue[i] +
                                            stoneValue[i + 1] +
                                            stoneValue[i + 2]);
}
else if (i == length - 2)
{
table[length - 2] = this.Max(stoneValue[i],
                                            stoneValue[i] + stoneValue[i + 1]);
}
else
{
table[length - 1] = stoneValue[i];
}
}

if ((float)table[0] > sum / 2.0)
{
return "Alice";
}
else if ((float)table[0] == sum / 2.0)
{
return "Tie";
}

return "Bob";
}

private int Max(params int[] values)
{
return Enumerable.Max(values);
}
private int Min(params int[] values)
{
return Enumerable.Min(values);
}


Complexity: O(n)

No comments:

Post a Comment