20210202 Finished

This commit is contained in:
jackfiled 2024-02-02 21:17:19 +08:00
parent d3c127480b
commit e27a6218c0
2 changed files with 60 additions and 1 deletions

View File

@ -35,3 +35,4 @@ mod p514_freedom_trail;
mod p2808_minimum_seconds_to_equalize_a_circular_array; mod p2808_minimum_seconds_to_equalize_a_circular_array;
mod p2670_find_the_distinct_difference_array; mod p2670_find_the_distinct_difference_array;
mod lcp24_nums_game; mod lcp24_nums_game;
mod p1686_stone_game_vi;

View File

@ -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<i32>, bob_values: Vec<i32>) -> i32 {
let mut stones: Vec<Stone> = 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);
}
}