class Solution:
def stoneGameVI(self, aliceValues, bobValues):
n = len(aliceValues)
stones = sorted(
zip(aliceValues, bobValues),
key=lambda x: -(x[0] + x[1])
)
alice, bob = 0, 0
for i, (a, b) in enumerate(stones):
if i % 2 == 0:
alice += a
else:
bob += b
if alice > bob:
return 1
elif alice < bob:
return -1
else:
return 0
class Solution {
public:
int stoneGameVI(vector<int>& aliceValues, vector<int>& bobValues) {
int n = aliceValues.size();
vector<pair<int, pair<int, int>>> stones;
for (int i = 0; i < n; ++i) {
stones.push_back({aliceValues[i] + bobValues[i], {aliceValues[i], bobValues[i]}});
}
sort(stones.rbegin(), stones.rend());
int alice = 0, bob = 0;
for (int i = 0; i < n; ++i) {
if (i % 2 == 0)
alice += stones[i].second.first;
else
bob += stones[i].second.second;
}
if (alice > bob) return 1;
else if (alice < bob) return -1;
else return 0;
}
};
class Solution {
public int stoneGameVI(int[] aliceValues, int[] bobValues) {
int n = aliceValues.length;
int[][] stones = new int[n][3];
for (int i = 0; i < n; i++) {
stones[i][0] = aliceValues[i] + bobValues[i];
stones[i][1] = aliceValues[i];
stones[i][2] = bobValues[i];
}
Arrays.sort(stones, (a, b) -> b[0] - a[0]);
int alice = 0, bob = 0;
for (int i = 0; i < n; i++) {
if (i % 2 == 0)
alice += stones[i][1];
else
bob += stones[i][2];
}
if (alice > bob) return 1;
else if (alice < bob) return -1;
else return 0;
}
}
var stoneGameVI = function(aliceValues, bobValues) {
const n = aliceValues.length;
let stones = [];
for (let i = 0; i < n; i++) {
stones.push([aliceValues[i] + bobValues[i], aliceValues[i], bobValues[i]]);
}
stones.sort((a, b) => b[0] - a[0]);
let alice = 0, bob = 0;
for (let i = 0; i < n; i++) {
if (i % 2 === 0) alice += stones[i][1];
else bob += stones[i][2];
}
if (alice > bob) return 1;
else if (alice < bob) return -1;
else return 0;
};
aliceValues
and bobValues
, each of length n
. There are n
stones, and the i
-th stone has a value aliceValues[i]
for Alice and bobValues[i]
for Bob. Alice and Bob take turns picking up stones (Alice starts first), and once a stone is picked, it cannot be chosen again. Each player adds the value of the stone they pick (according to their own array) to their score. The game continues until all stones are taken. The goal is to determine who will win if both play optimally:
1
if Alice wins (her score is higher)-1
if Bob wins0
if it's a tien
grows, because the number of possible pick orders is n!
(factorial time).aliceValues[i] + bobValues[i]
) tells us how much a stone is "contested" between the two players. Picking stones with the highest combined value first ensures that the most valuable options are taken early, minimizing what the opponent can gain from them.
aliceValues[i] + bobValues[i]
. This ensures that, on each turn, the player picks the stone that is most valuable overall.
aliceValues[i]
to her score.bobValues[i]
to his score.This greedy approach works because picking the highest combined value stone either maximizes your gain or minimizes your opponent's possible gain, which is exactly what optimal play requires in this context.
Let's consider the example:
aliceValues = [1, 3]
bobValues = [2, 1]
1
.
This process ensures that the player who can get the most out of the available stones does so, while also denying the opponent their best options.
This is efficient and suitable for the problem's constraints.