diff --git a/src/problem/mod.rs b/src/problem/mod.rs index c853fda..b4b7361 100644 --- a/src/problem/mod.rs +++ b/src/problem/mod.rs @@ -446,3 +446,5 @@ mod p2266_count_number_of_texts; mod p2239_find_closest_number_to_zero; mod p2218_maximum_value_of_k_coins_from_piles; + +mod p1561_maximum_number_of_coins_you_can_get; diff --git a/src/problem/p1561_maximum_number_of_coins_you_can_get.rs b/src/problem/p1561_maximum_number_of_coins_you_can_get.rs new file mode 100644 index 0000000..af891ea --- /dev/null +++ b/src/problem/p1561_maximum_number_of_coins_you_can_get.rs @@ -0,0 +1,35 @@ +/** + * [1561] Maximum Number of Coins You Can Get + */ +pub struct Solution {} + +// submission codes start here + +impl Solution { + pub fn max_coins(mut piles: Vec) -> i32 { + let n = piles.len() / 3; + piles.sort_unstable_by(|a, b| b.cmp(a)); + + let mut result = 0; + + for i in (0..n).map(|x| x * 2 + 1) { + result += piles[i] + } + + result + } +} + +// submission codes end + +#[cfg(test)] +mod tests { + use super::*; + + #[test] + fn test_1561() { + assert_eq!(9, Solution::max_coins(vec![2, 4, 1, 2, 7, 8])); + assert_eq!(4, Solution::max_coins(vec![2, 4, 5])); + assert_eq!(18, Solution::max_coins(vec![9, 8, 7, 6, 5, 1, 2, 3, 4])); + } +}