1686. Stone Game VI LeetCode Solution
In this guide, you will get 1686. Stone Game VI LeetCode Solution with the best time and space complexity. The solution to Stone Game VI problem is provided in various programming languages like C++, Java, and Python. This will be helpful for you if you are preparing for placements, hackathons, interviews, or practice purposes. The solutions provided here are very easy to follow and include detailed explanations.
Table of Contents
- Problem Statement
- Complexity Analysis
- Stone Game VI solution in C++
- Stone Game VI solution in Java
- Stone Game VI solution in Python
- Additional Resources

Problem Statement of Stone Game VI
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.
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.
Example 1:
Input: aliceValues = [1,3], bobValues = [2,1]
Output: 1
Explanation:
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.
Example 2:
Input: aliceValues = [1,2], bobValues = [3,1]
Output: 0
Explanation:
If Alice takes stone 0, and Bob takes stone 1, they will both have 1 point.
Draw.
Example 3:
Input: aliceValues = [2,4,3], bobValues = [1,6,7]
Output: -1
Explanation:
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.
Constraints:
n == aliceValues.length == bobValues.length
1 <= n <= 105
1 <= aliceValues[i], bobValues[i] <= 100
Complexity Analysis
- Time Complexity: O(\texttt{sort})
- Space Complexity: O(n)
1686. Stone Game VI LeetCode Solution in C++
class Solution {
public:
int stoneGameVI(vector<int>& aliceValues, vector<int>& bobValues) {
const int n = aliceValues.size();
vector<vector<int>> values;
int a = 0;
int b = 0;
for (int i = 0; i < n; ++i)
values.push_back({aliceValues[i], bobValues[i]});
ranges::sort(values, ranges::greater{},
[](const vector<int>& value) { return value[0] + value[1]; });
for (int i = 0; i < n; ++i)
if (i % 2 == 0)
a += values[i][0];
else
b += values[i][1];
return a > b ? 1 : a < b ? -1 : 0;
}
};
/* code provided by PROGIEZ */
1686. Stone Game VI LeetCode Solution in Java
class Solution {
public int stoneGameVI(int[] aliceValues, int[] bobValues) {
final int n = aliceValues.length;
int[][] values = new int[n][];
for (int i = 0; i < n; ++i)
values[i] = new int[] {aliceValues[i], bobValues[i]};
Arrays.sort(values, (a, b) -> Integer.compare(b[0] + b[1], a[0] + a[1]));
int a = 0;
int b = 0;
for (int i = 0; i < n; ++i)
if (i % 2 == 0)
a += values[i][0];
else
b += values[i][1];
return Integer.compare(a, b);
}
}
// code provided by PROGIEZ
1686. Stone Game VI LeetCode Solution in Python
N/A
# code by PROGIEZ
Additional Resources
- Explore all LeetCode problem solutions at Progiez here
- Explore all problems on LeetCode website here
Happy Coding! Keep following PROGIEZ for more updates and solutions.