1686. 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.

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

Rust Solution

struct Solution;
use std::cmp::Ordering::*;

impl Solution {
    fn stone_game_vi(alice_values: Vec<i32>, bob_values: Vec<i32>) -> i32 {
        let n = alice_values.len();
        let mut sums = vec![];
        for i in 0..n {
            sums.push((
                alice_values[i] + bob_values[i],
                alice_values[i],
                bob_values[i],
            ));
        }
        sums.sort_unstable();
        let mut sum_a = 0;
        let mut sum_b = 0;
        let mut turn = 0;
        while let Some((_, a, b)) = sums.pop() {
            if turn % 2 == 0 {
                sum_a += a;
            } else {
                sum_b += b;
            }
            turn += 1;
        }
        match sum_a.cmp(&sum_b) {
            Greater => 1,
            Less => -1,
            Equal => 0,
        }
    }
}

#[test]
fn test() {
    let alice_values = vec![1, 3];
    let bob_values = vec![2, 1];
    let res = 1;
    assert_eq!(Solution::stone_game_vi(alice_values, bob_values), res);
    let alice_values = vec![1, 2];
    let bob_values = vec![3, 1];
    let res = 0;
    assert_eq!(Solution::stone_game_vi(alice_values, bob_values), res);
    let alice_values = vec![2, 4, 3];
    let bob_values = vec![1, 6, 7];
    let res = -1;
    assert_eq!(Solution::stone_game_vi(alice_values, bob_values), res);
}

Having problems with this solution? Click here to submit an issue on github.