Skip to content

Commit

Permalink
[algorithms]添加贪心求解最少硬币数量问题的示例
Browse files Browse the repository at this point in the history
  • Loading branch information
Trackerming committed Jul 11, 2024
1 parent 3f50fef commit 1468c63
Show file tree
Hide file tree
Showing 2 changed files with 43 additions and 0 deletions.
41 changes: 41 additions & 0 deletions algorithms/src/greed.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
// 给定n种硬币,第i种硬币的面值为 coins[i-1],目标金额为amt,
// 每种硬币可以重复选取,问能够凑出目标金额的**最少**硬币数量。如果无法凑出目标金额,则返回-1

pub fn coin_change_greedy(coins: &mut [i32], mut amt: i32) -> i32 {
// 让coins有序
coins.sort();
let mut index = coins.len() - 1;
let mut count = 0;
// 还没有选取完毕,就继续循环贪心选取
while amt > 0 {
// 找到小于且最接近amt的那个coin
while index > 0 && coins[index] > amt {
index -= 1;
}
// 选择coins[index]
amt -= coins[index];
count += 1;
}
if amt == 0 {
count
} else {
-1
}
}

#[cfg(test)]
mod greedy_test {
use super::*;

#[test]
fn test_coin_change_greedy() {
let mut coins = [1, 5, 10, 20, 50, 100];
let count = coin_change_greedy(&mut coins, 138);
assert_eq!(count, 7);
// 贪心不适用的特例
let mut coins = [1, 20, 50];
// 动态规划可以找到20 + 20 + 20
let count = coin_change_greedy(&mut coins, 60);
assert_eq!(count, 11);
}
}
2 changes: 2 additions & 0 deletions algorithms/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -12,3 +12,5 @@ pub mod two_sum;
pub mod hash_map;

pub mod bit_op;

pub mod greed;

0 comments on commit 1468c63

Please sign in to comment.