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) + ); + } +}