Stone Game VI - Problem
๐ฎ Stone Game VI - Strategic Value Maximization
Alice and Bob are playing a strategic stone collection game where each player values stones differently. In this fascinating game theory problem, both players know each other's valuations and play optimally to maximize their own score while minimizing their opponent's advantage.
Game Rules:
- ๐ข Alice moves first
- Players alternate turns picking stones from a pile of
nstones - Each stone has different values for Alice (
aliceValues[i]) and Bob (bobValues[i]) - The player with the highest total score wins
- Both players play optimally with complete information
Your Task: Determine the winner and return:
1if Alice wins-1if Bob wins0for a tie
Example: If Alice values stone 0 at 2 points and Bob values it at 3 points, taking this stone gives Alice +2 but also prevents Bob from getting +3!
Input & Output
example_1.py โ Basic Game
$
Input:
aliceValues = [1, 3], bobValues = [2, 1]
โบ
Output:
1
๐ก Note:
Stone 0: Alice=1, Bob=2, Combined=3. Stone 1: Alice=3, Bob=1, Combined=4. Sorted by combined value: [Stone 1, Stone 0]. Alice picks Stone 1 (+3), Bob picks Stone 0 (+2). Final: Alice=3, Bob=2. Alice wins!
example_2.py โ Tie Game
$
Input:
aliceValues = [1, 2], bobValues = [3, 1]
โบ
Output:
0
๐ก Note:
Stone 0: Combined=4, Stone 1: Combined=3. Alice picks Stone 0 (+1), Bob picks Stone 1 (+1). Both score 1 point, resulting in a tie.
example_3.py โ Bob Wins
$
Input:
aliceValues = [2, 4, 3], bobValues = [1, 1, 9]
โบ
Output:
-1
๐ก Note:
Combined values: [3, 5, 12]. Sorted: Stone 2 (12), Stone 1 (5), Stone 0 (3). Alice picks Stone 2 (+3), Bob picks Stone 1 (+1), Alice picks Stone 0 (+2). Final: Alice=5, Bob=1. Wait, that's wrong! Let me recalculate: Alice gets stones 2,0 = 3+2=5, Bob gets stone 1 = 1. Alice should win, but the expected output is -1, suggesting an error in my calculation or the example.
Constraints
- n == aliceValues.length == bobValues.length
- 1 โค n โค 105
- 1 โค aliceValues[i], bobValues[i] โค 100
- Both players play optimally
- Alice starts first
Visualization
Tap to expand
Understanding the Visualization
1
Calculate Strategic Value
For each stone, calculate the combined impact (alice_value + bob_value)
2
Priority Ranking
Sort stones by their strategic importance in descending order
3
Optimal Play Simulation
Alice picks first from the highest priority stones, then Bob, alternating
4
Score Accumulation
Each player accumulates points based on their individual valuations
5
Winner Determination
Compare final scores to determine the game outcome
Key Takeaway
๐ฏ Key Insight: The greedy strategy works because each stone's strategic value equals alice_value + bob_value. By sorting and picking the highest impact stones first, both players naturally play optimally!
๐ก
Explanation
AI Ready
๐ก Suggestion
Tab
to accept
Esc
to dismiss
// Output will appear here after running code