20240330 Finished

This commit is contained in:
jackfiled 2024-03-30 12:06:47 +08:00
parent 9e7e56f276
commit 2635eeb818
2 changed files with 47 additions and 1 deletions

View File

@ -89,3 +89,4 @@ 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;
mod p2952_minimum_number_of_coins_to_be_added;

View File

@ -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<i32>, 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)
);
}
}