/* * 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 */ fn fractional_knapsack(wgt: &[i32], val: &[i32], mut cap: i32) -> f64 { // Create an item list, containing two properties: weight, value let mut items = wgt .iter() .zip(val.iter()) .map(|(&w, &v)| Item::new(w, v)) .collect::>(); // 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 the remaining capacity is sufficient, put the entire item into the knapsack res += item.v as f64; cap -= item.w; } else { // If the remaining capacity is insufficient, put part of the item into the knapsack res += item.v as f64 / item.w as f64 * cap as f64; // No remaining capacity left, thus break 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!("The maximum value within the bag capacity is {}", res); }