1686. Stone Game VI

Description

Alice and Bob take turns playing a game, with Alice starting first.

There are n stones in a pile. On each player's turn, they can remove a stone from the pile and receive points based on the stone's value. Alice and Bob may value the stones differently.

You are given two integer arrays of length n, aliceValues and bobValues. Each aliceValues[i] and bobValues[i] represents how Alice and Bob, respectively, value the ith stone.

The winner is the person with the most points after all the stones are chosen. If both players have the same amount of points, the game results in a draw. Both players will play optimally. Both players know the other's values.

Determine the result of the game, and:

  • If Alice wins, return 1.
  • If Bob wins, return -1.
  • If the game results in a draw, return 0.

 

Example 1:

Input: aliceValues = [1,3], bobValues = [2,1]
Output: 1
Explanation:
If Alice takes stone 1 (0-indexed) first, Alice will receive 3 points.
Bob can only choose stone 0, and will only receive 2 points.
Alice wins.

Example 2:

Input: aliceValues = [1,2], bobValues = [3,1]
Output: 0
Explanation:
If Alice takes stone 0, and Bob takes stone 1, they will both have 1 point.
Draw.

Example 3:

Input: aliceValues = [2,4,3], bobValues = [1,6,7]
Output: -1
Explanation:
Regardless of how Alice plays, Bob will be able to have more points than Alice.
For example, if Alice takes stone 1, Bob can take stone 2, and Alice takes stone 0, Alice will have 6 points to Bob's 7.
Bob wins.

 

Constraints:

  • n == aliceValues.length == bobValues.length
  • 1 <= n <= 105
  • 1 <= aliceValues[i], bobValues[i] <= 100

Solutions

Solution 1: Greedy + Sorting

The optimal strategy for picking stones is to maximize one’s own score while making the opponent lose as much as possible. Therefore, we create an array arr, where arr[i] = aliceValues[i] + bobValues[i], and then sort arr in descending order. Then, we take stones from arr, taking two stones each time, one for Alice and one for Bob, until there are no stones left in arr. Finally, we compare the scores of Alice and Bob, and the person with the higher score wins.

The time complexity is $O(n \times \log n)$, and the space complexity is $O(n)$. Here, $n$ is the length of the arrays aliceValues and bobValues.

Python Code
 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
class Solution:
    def stoneGameVI(self, aliceValues: List[int], bobValues: List[int]) -> int:
        arr = [(a + b, i) for i, (a, b) in enumerate(zip(aliceValues, bobValues))]
        arr.sort(reverse=True)
        a = sum(aliceValues[v[1]] for i, v in enumerate(arr) if i % 2 == 0)
        b = sum(bobValues[v[1]] for i, v in enumerate(arr) if i % 2 == 1)
        if a > b:
            return 1
        if a < b:
            return -1
        return 0

Java Code
 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
class Solution {
    public int stoneGameVI(int[] aliceValues, int[] bobValues) {
        int n = aliceValues.length;
        int[][] arr = new int[n][2];
        for (int i = 0; i < n; ++i) {
            arr[i] = new int[] {aliceValues[i] + bobValues[i], i};
        }
        Arrays.sort(arr, (a, b) -> b[0] - a[0]);
        int a = 0, b = 0;
        for (int i = 0; i < n; ++i) {
            int j = arr[i][1];
            if (i % 2 == 0) {
                a += aliceValues[j];
            } else {
                b += bobValues[j];
            }
        }
        if (a == b) {
            return 0;
        }
        return a > b ? 1 : -1;
    }
}

C++ Code
 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
class Solution {
public:
    int stoneGameVI(vector<int>& aliceValues, vector<int>& bobValues) {
        int n = aliceValues.size();
        vector<pair<int, int>> arr(n);
        for (int i = 0; i < n; ++i) {
            arr[i] = {aliceValues[i] + bobValues[i], i};
        }
        sort(arr.rbegin(), arr.rend());
        int a = 0, b = 0;
        for (int i = 0; i < n; ++i) {
            int j = arr[i].second;
            if (i % 2 == 0) {
                a += aliceValues[j];
            } else {
                b += bobValues[j];
            }
        }
        if (a == b) return 0;
        return a > b ? 1 : -1;
    }
};

Go Code
 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
func stoneGameVI(aliceValues []int, bobValues []int) int {
	arr := make([][]int, len(aliceValues))
	for i, a := range aliceValues {
		b := bobValues[i]
		arr[i] = []int{a + b, i}
	}
	sort.Slice(arr, func(i, j int) bool { return arr[i][0] > arr[j][0] })
	a, b := 0, 0
	for i, v := range arr {
		if i%2 == 0 {
			a += aliceValues[v[1]]
		} else {
			b += bobValues[v[1]]
		}
	}
	if a == b {
		return 0
	}
	if a > b {
		return 1
	}
	return -1
}