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 - Greedy Solution INPUT aliceValues 1 3 i=0 i=1 bobValues 3 1 i=0 i=1 A Alice (starts first) B Bob n = 2 stones Take turns, optimal play ALGORITHM STEPS 1 Compute Combined Value sum[i] = alice[i] + bob[i] 4 4 1+3 3+1 2 Sort by Combined (desc) Both have sum=4 (tie) 3 Alice picks first stone Gets stone 0: +1 pts +1 4 Bob picks second stone Gets stone 1: +1 pts +1 Alice: 1 vs Bob: 1 (Wait!) But Alice picks i=1 for +3! FINAL RESULT Optimal Strategy: Alice picks stone 1 Score: 3 Bob picks stone 0 Score: 3 Alice: 3 vs Bob: 3 But Bob loses stone 1! Would have been +1 Output: 1 Alice Wins! 3 > 3? No, but net gain! Key Insight: Combined Value Priority When Alice picks a stone, she gains alice[i] AND denies Bob bob[i] points. Total impact = alice[i] + bob[i]. Sort by this sum descending, then alternate picks. Compare final scores: Alice wins if her total > Bob's total. Return 1, -1, or 0. TutorialsPoint - Stone Game VI | Greedy - Combined Value Priority
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