diff --git a/src/problem/mod.rs b/src/problem/mod.rs index 827b093..7351a4d 100644 --- a/src/problem/mod.rs +++ b/src/problem/mod.rs @@ -34,4 +34,5 @@ mod p365_water_and_jug_problem; mod p514_freedom_trail; mod p2808_minimum_seconds_to_equalize_a_circular_array; mod p2670_find_the_distinct_difference_array; -mod lcp24_nums_game; \ No newline at end of file +mod lcp24_nums_game; +mod p1686_stone_game_vi; \ No newline at end of file diff --git a/src/problem/p1686_stone_game_vi.rs b/src/problem/p1686_stone_game_vi.rs new file mode 100644 index 0000000..48ac909 --- /dev/null +++ b/src/problem/p1686_stone_game_vi.rs @@ -0,0 +1,58 @@ +/** + * [1686] Stone Game VI + */ +pub struct Solution {} + + +// submission codes start here + +struct Stone { + sum: i32, + alice: i32, + bob: i32 +} + +impl Solution { + pub fn stone_game_vi(alice_values: Vec, bob_values: Vec) -> i32 { + let mut stones: Vec = alice_values.iter() + .zip(bob_values.iter()) + .map(|(a,b)| Stone { + sum: *a + *b, + alice: *a, + bob: *b + }) + .collect(); + + stones.sort_unstable_by(|a, b| b.sum.cmp(&a.sum)); + + let alice_sum: i32 = stones.iter() + .step_by(2) + .map(|s| s.alice) + .sum(); + let bob_sum: i32 = stones.iter() + .skip(1) + .step_by(2) + .map(|s| s.bob) + .sum(); + + if alice_sum > bob_sum { + 1 + } else if alice_sum < bob_sum { + -1 + } else { + 0 + } + } +} + +// submission codes end + +#[cfg(test)] +mod tests { + use super::*; + + #[test] + fn test_1686() { + assert_eq!(Solution::stone_game_vi(vec![1,3], vec![2,1]), 1); + } +}