Skip to main content

Command Palette

Search for a command to run...

Solving Leetcode Interviews in Seconds with AI: Stone Game VI

Updated
3 min read

Introduction

In this blog post, we will explore how to solve the LeetCode problem "1686" using AI. LeetCode is a popular platform for preparing for coding interviews, and with the help of AI tools like Chatmagic, we can generate solutions quickly and efficiently - helping you pass the interviews and get the job offer without having to study for months.

Problem Statement

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

Explanation

Here's a breakdown of the approach, followed by the code:

  • Calculate the Difference: The key insight is to consider the difference between Alice's and Bob's values for each stone (i.e., aliceValues[i] - bobValues[i]). If this difference is positive, it means Alice benefits more from taking the stone than Bob. If it's negative, Bob benefits more.

  • Greedy Selection: Sort the stones based on this difference in descending order. This way, we prioritize stones where Alice gains the most advantage (or loses the least disadvantage) compared to Bob. Alice and Bob then take turns picking the stones in this sorted order.

  • Determine the Winner: Keep track of Alice's and Bob's scores. After all stones are taken, compare the scores and return 1, -1, or 0 accordingly.

  • Complexity: O(n log n) time (due to sorting), O(n) space (for storing indices and the difference array).

Code

    def stoneGameVI(aliceValues, bobValues):
    """
    Determines the winner of the stone game.

    Args:
        aliceValues: A list of integers representing Alice's values for each stone.
        bobValues: A list of integers representing Bob's values for each stone.

    Returns:
        1 if Alice wins, -1 if Bob wins, 0 if it's a draw.
    """

    n = len(aliceValues)
    diffs = []
    for i in range(n):
        diffs.append((aliceValues[i] + bobValues[i], i))  # Store sum and original index

    diffs.sort(reverse=True)

    alice_score = 0
    bob_score = 0

    for i in range(n):
        stone_index = diffs[i][1]
        if i % 2 == 0:  # Alice's turn
            alice_score += aliceValues[stone_index]
        else:  # Bob's turn
            bob_score += bobValues[stone_index]

    if alice_score > bob_score:
        return 1
    elif alice_score < bob_score:
        return -1
    else:
        return 0

More from this blog

C

Chatmagic blog

2894 posts