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.
Input: aliceValues =[1,3], bobValues =[2,1]Output: 1Explanation:
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.
Input: aliceValues =[2,4,3], bobValues =[1,6,7]Output: -1Explanation:
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.
Each player wants to maximize their own score, but since both know each other’s values, the optimal strategy is to always pick the stone with the highest combined value (aliceValues[i] + bobValues[i]) on each turn. This way, the player can maximize their own gain and minimize the opponent’s gain.
#include<vector>#include<algorithm>usingnamespace std;
classSolution {
public:int stoneGameVI(vector<int>& aliceValues, vector<int>& bobValues) {
int n = aliceValues.size();
vector<pair<int, int>> stones;
for (int i =0; i < n; ++i)
stones.push_back({aliceValues[i] + bobValues[i], i});
sort(stones.rbegin(), stones.rend());
int alice =0, bob =0;
for (int i =0; i < n; ++i) {
int idx = stones[i].second;
if (i %2==0) alice += aliceValues[idx];
else bob += bobValues[idx];
}
if (alice > bob) return1;
if (alice < bob) return-1;
return0;
}
};
import java.util.*;
classSolution {
publicintstoneGameVI(int[] aliceValues, int[] bobValues) {
int n = aliceValues.length;
int[][] stones =newint[n][2];
for (int i = 0; i < n; i++) {
stones[i][0]= aliceValues[i]+ bobValues[i];
stones[i][1]= i;
}
Arrays.sort(stones, (a, b) -> b[0]- a[0]);
int alice = 0, bob = 0;
for (int i = 0; i < n; i++) {
int idx = stones[i][1];
if (i % 2 == 0) alice += aliceValues[idx];
else bob += bobValues[idx];
}
if (alice > bob) return 1;
if (alice < bob) return-1;
return 0;
}
}
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
funstoneGameVI(aliceValues: IntArray, bobValues: IntArray): Int {
val n = aliceValues.size
val stones = List(n) { aliceValues[it] + bobValues[it] to it }.sortedByDescending { it.first }
var alice = 0; var bob = 0for (i in0 until n) {
val idx = stones[i].second
if (i % 2==0) alice += aliceValues[idx]
else bob += bobValues[idx]
}
returnwhen {
alice > bob ->1 alice < bob -> -1else->0 }
}
1
2
3
4
5
6
7
8
9
10
11
12
13
defstoneGameVI(aliceValues: list[int], bobValues: list[int]) -> int:
stones = sorted([(a+b, i) for i, (a, b) in enumerate(zip(aliceValues, bobValues))], reverse=True)
alice = bob =0for i, (_, idx) in enumerate(stones):
if i %2==0:
alice += aliceValues[idx]
else:
bob += bobValues[idx]
if alice > bob:
return1if alice < bob:
return-1return0
1
2
3
4
5
6
7
8
9
10
11
12
13
14
pubfnstone_game_vi(alice_values: Vec<i32>, bob_values: Vec<i32>) -> i32 {
let n = alice_values.len();
letmut stones: Vec<(i32, usize)>= (0..n).map(|i| (alice_values[i] + bob_values[i], i)).collect();
stones.sort_unstable_by(|a, b| b.0.cmp(&a.0));
let (mut alice, mut bob) = (0, 0);
for (i, &(_, idx)) in stones.iter().enumerate() {
if i %2==0 {
alice += alice_values[idx];
} else {
bob += bob_values[idx];
}
}
if alice > bob { 1 } elseif alice < bob { -1 } else { 0 }
}
1
2
3
4
5
6
7
8
9
10
11
12
13
14
functionstoneGameVI(aliceValues: number[], bobValues: number[]):number {
constn=aliceValues.length;
conststones= Array.from({length: n}, (_, i) => [aliceValues[i] +bobValues[i], i]);
stones.sort((a, b) =>b[0] -a[0]);
letalice=0, bob=0;
for (leti=0; i<n; i++) {
constidx=stones[i][1];
if (i%2===0) alice+=aliceValues[idx];
elsebob+=bobValues[idx];
}
if (alice>bob) return1;
if (alice<bob) return-1;
return0;
}