From 2635eeb818c5fa63010aca8b6b58fed2987f2fa4 Mon Sep 17 00:00:00 2001 From: jackfiled Date: Sat, 30 Mar 2024 12:06:47 +0800 Subject: [PATCH] 20240330 Finished --- src/problem/mod.rs | 3 +- ...952_minimum_number_of_coins_to_be_added.rs | 45 +++++++++++++++++++ 2 files changed, 47 insertions(+), 1 deletion(-) create mode 100644 src/problem/p2952_minimum_number_of_coins_to_be_added.rs diff --git a/src/problem/mod.rs b/src/problem/mod.rs index 971e2d2..29f5538 100644 --- a/src/problem/mod.rs +++ b/src/problem/mod.rs @@ -88,4 +88,5 @@ mod p518_coin_change_ii; mod p2642_design_graph_with_shortest_path_calculator; mod p2580_count_ways_to_group_overlapping_ranges; mod p1997_first_day_where_you_have_been_in_all_the_rooms; -mod p2908_minimum_sum_of_mountain_triplets_i; \ No newline at end of file +mod p2908_minimum_sum_of_mountain_triplets_i; +mod p2952_minimum_number_of_coins_to_be_added; \ No newline at end of file diff --git a/src/problem/p2952_minimum_number_of_coins_to_be_added.rs b/src/problem/p2952_minimum_number_of_coins_to_be_added.rs new file mode 100644 index 0000000..3e29cb5 --- /dev/null +++ b/src/problem/p2952_minimum_number_of_coins_to_be_added.rs @@ -0,0 +1,45 @@ +/** + * [2952] Minimum Number of Coins to be Added + */ +pub struct Solution {} + +// submission codes start here + +impl Solution { + pub fn minimum_added_coins(coins: Vec, target: i32) -> i32 { + let mut coins = coins; + coins.sort_unstable(); + + let mut result = 0; + let mut i = 0; + let mut x = 1; + + while x <= target { + if i < coins.len() && coins[i] <= x { + x += coins[i]; + i += 1; + } else { + x = x * 2; + result += 1; + } + } + + result + } +} + +// submission codes end + +#[cfg(test)] +mod tests { + use super::*; + + #[test] + fn test_2952() { + assert_eq!(2, Solution::minimum_added_coins(vec![1, 4, 10], 19)); + assert_eq!( + 0, + Solution::minimum_added_coins(vec![1, 2, 4, 6, 7, 9, 9, 10], 48) + ); + } +}