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 i-th 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.

Input & Output

Example 1 — Alice Wins
$ Input: aliceValues = [1,3], bobValues = [3,1]
Output: 1
💡 Note: Combined values: [4,4]. Alice picks first stone (gains 1), Bob picks second (gains 1). Alice: 1, Bob: 1. Tie, so result is 0. Wait - let me recalculate: If Alice picks stone 0 (A:1,B:3), then Bob picks stone 1 (A:3,B:1) and gets 1 point. Alice:1, Bob:1 = tie. But if Alice picks stone 1 first (A:3,B:3), Bob gets stone 0 (A:1,B:3) and gets 3. Alice:3, Bob:3 = tie. Actually both lead to tie, so result is 0.
Example 2 — Different Values
$ Input: aliceValues = [1,2], bobValues = [3,1]
Output: 0
💡 Note: Combined values: stone 0 has sum 4, stone 1 has sum 3. Alice picks stone 0 (gains 1), Bob picks stone 1 (gains 1). Alice: 1, Bob: 1. Draw.
Example 3 — Clear Winner
$ Input: aliceValues = [2,4,3], bobValues = [1,1,2]
Output: 1
💡 Note: Combined values: [3,5,5]. Sorted order: stone 1 (sum 5), stone 2 (sum 5), stone 0 (sum 3). Alice picks stone 1 (gains 4), Bob picks stone 2 (gains 2), Alice picks stone 0 (gains 2). Alice: 6, Bob: 2. Alice wins.

Constraints

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

Visualization

Tap to expand
Stone Game VI: Optimal Stone SelectionAlice Values243Bob Values112Optimal PlayGame SimulationCombined Values: [3, 5, 5]Sort by priority: Stone 1 (sum=5), Stone 2 (sum=5), Stone 0 (sum=3)Turn 1: Alice picks Stone 1 → +4Turn 2: Bob picks Stone 2 → +2Turn 3: Alice picks Stone 0 → +2Alice: 6 pointsBob: 2 pointsAlice Wins!Return: 1🎯 Key Insight: Pick stones with highest combined impact (your gain + opponent's loss)
Understanding the Visualization
1
Input
Two arrays showing how Alice and Bob value each stone
2
Process
Players alternate turns picking stones optimally
3
Output
1 if Alice wins, -1 if Bob wins, 0 for draw
Key Takeaway
🎯 Key Insight: The optimal strategy is to prioritize stones by their combined value impact - maximizing your gain while minimizing opponent's potential gain
Asked in
Google 12 Amazon 8 Facebook 6
25.4K Views
Medium Frequency
~25 min Avg. Time
892 Likes
Ln 1, Col 1
Smart Actions
💡 Explanation
AI Ready
💡 Suggestion Tab to accept Esc to dismiss
// Output will appear here after running code
Code Editor Closed
Click the red button to reopen