mirror of
https://github.com/krahets/hello-algo.git
synced 2026-05-04 12:54:12 +08:00
* Review the EN heading format. * Fix pythontutor headings. * Fix pythontutor headings. * bug fixes * Fix headings in **/summary.md * Revisit the CN-to-EN translation for Python code using Claude-4.5 * Revisit the CN-to-EN translation for Java code using Claude-4.5 * Revisit the CN-to-EN translation for Cpp code using Claude-4.5. * Fix the dictionary. * Fix cpp code translation for the multipart strings. * Translate Go code to English. * Update workflows to test EN code. * Add EN translation for C. * Add EN translation for CSharp. * Add EN translation for Swift. * Trigger the CI check. * Revert. * Update en/hash_map.md * Add the EN version of Dart code. * Add the EN version of Kotlin code. * Add missing code files. * Add the EN version of JavaScript code. * Add the EN version of TypeScript code. * Fix the workflows. * Add the EN version of Ruby code. * Add the EN version of Rust code. * Update the CI check for the English version code. * Update Python CI check. * Fix cmakelists for en/C code. * Fix Ruby comments
60 lines
1.6 KiB
Rust
60 lines
1.6 KiB
Rust
/*
|
|
* File: coin_change_greedy.rs
|
|
* Created Time: 2023-07-22
|
|
* Author: night-cruise (2586447362@qq.com)
|
|
*/
|
|
|
|
/* Item */
|
|
struct Item {
|
|
w: i32, // Item weight
|
|
v: i32, // Item value
|
|
}
|
|
|
|
impl Item {
|
|
fn new(w: i32, v: i32) -> Self {
|
|
Self { w, v }
|
|
}
|
|
}
|
|
|
|
/* Fractional knapsack: Greedy algorithm */
|
|
fn fractional_knapsack(wgt: &[i32], val: &[i32], mut cap: i32) -> f64 {
|
|
// Create item list with two attributes: weight, value
|
|
let mut items = wgt
|
|
.iter()
|
|
.zip(val.iter())
|
|
.map(|(&w, &v)| Item::new(w, v))
|
|
.collect::<Vec<Item>>();
|
|
// Sort by unit value item.v / item.w from high to low
|
|
items.sort_by(|a, b| {
|
|
(b.v as f64 / b.w as f64)
|
|
.partial_cmp(&(a.v as f64 / a.w as f64))
|
|
.unwrap()
|
|
});
|
|
// Loop for greedy selection
|
|
let mut res = 0.0;
|
|
for item in &items {
|
|
if item.w <= cap {
|
|
// If remaining capacity is sufficient, put the entire current item into the knapsack
|
|
res += item.v as f64;
|
|
cap -= item.w;
|
|
} else {
|
|
// If remaining capacity is insufficient, put part of the current item into the knapsack
|
|
res += item.v as f64 / item.w as f64 * cap as f64;
|
|
// No remaining capacity, so break out of the loop
|
|
break;
|
|
}
|
|
}
|
|
res
|
|
}
|
|
|
|
/* Driver Code */
|
|
fn main() {
|
|
let wgt = [10, 20, 30, 40, 50];
|
|
let val = [50, 120, 150, 210, 240];
|
|
let cap = 50;
|
|
|
|
// Greedy algorithm
|
|
let res = fractional_knapsack(&wgt, &val, cap);
|
|
println!("Maximum item value not exceeding knapsack capacity is {}", res);
|
|
}
|