LeetCode 1686: Stone Game VI Solution

Master LeetCode problem 1686 (Stone Game VI), a medium challenge, with our optimized solutions in Java, C++, and Python. Explore detailed explanations, test your code in our interactive editor, and prepare for coding interviews.

Problem Explanation

Explanation:

To solve this problem, we can sort the stones by the sum of their values in decreasing order. Then, Alice and Bob can greedily pick stones based on the sorted order, maximizing their points. We can keep track of the accumulated points for Alice and Bob, and determine the winner based on the total points earned by both players.

  • Sort the stones by the sum of aliceValues[i] and bobValues[i] in decreasing order.
  • Iterate through the sorted stones, with Alice and Bob taking turns to pick stones.
  • Keep track of the total points accumulated by Alice and Bob.
  • Determine the winner based on the total points earned by both players.

Time Complexity: O(n log n)
Space Complexity: O(n)

:

Solution Code

class Solution {
    public int stoneGameVI(int[] aliceValues, int[] bobValues) {
        int n = aliceValues.length;
        int[][] stones = new int[n][3]; // [sum, aliceValue, bobValue]
        
        for (int i = 0; i < n; i++) {
            stones[i] = new int[]{aliceValues[i] + bobValues[i], aliceValues[i], bobValues[i]};
        }
        
        Arrays.sort(stones, (a, b) -> Integer.compare(b[0], a[0]));
        
        int alicePoints = 0, bobPoints = 0;
        
        for (int i = 0; i < n; i++) {
            if (i % 2 == 0) {
                alicePoints += stones[i][1];
            } else {
                bobPoints += stones[i][2];
            }
        }
        
        if (alicePoints > bobPoints) return 1;
        else if (alicePoints < bobPoints) return -1;
        else return 0;
    }
}

Try It Yourself

Loading code editor...

Related LeetCode Problems

Frequently Asked Questions

How to solve LeetCode 1686 (Stone Game VI)?

This page provides optimized solutions for LeetCode problem 1686 (Stone Game VI) in Java, C++, and Python, along with a detailed explanation and an interactive code editor to test your code.

What is the time complexity of LeetCode 1686 (Stone Game VI)?

The time complexity for LeetCode 1686 (Stone Game VI) varies by solution. Check the detailed explanation section for specific complexities in Java, C++, and Python implementations.

Can I run code for LeetCode 1686 on DevExCode?

Yes, DevExCode provides an interactive code editor where you can write, test, and run your code for LeetCode 1686 in Java, C++, or Python.

Back to LeetCode Solutions