From e2e02d45446ff0df57f08a625358494f15fba4b4 Mon Sep 17 00:00:00 2001 From: "caitlin.gao" Date: Tue, 27 Aug 2019 18:58:41 +0800 Subject: [PATCH] feat(geektime_algo): add 40 dynamic programming bag, knapsack, triangle --- rust/40_dynamic_programming/bag.rs | 42 +++++++++++++++++++++++++ rust/40_dynamic_programming/knapsack.rs | 32 +++++++++++++++++++ rust/40_dynamic_programming/triangle.rs | 23 ++++++++++++++ 3 files changed, 97 insertions(+) create mode 100644 rust/40_dynamic_programming/bag.rs create mode 100644 rust/40_dynamic_programming/knapsack.rs create mode 100644 rust/40_dynamic_programming/triangle.rs diff --git a/rust/40_dynamic_programming/bag.rs b/rust/40_dynamic_programming/bag.rs new file mode 100644 index 0000000..cf62dc1 --- /dev/null +++ b/rust/40_dynamic_programming/bag.rs @@ -0,0 +1,42 @@ +fn knapsack(items: Vec, capacity: i32) -> i32 { + let mut states = vec![vec![false; (capacity + 1) as usize]; items.len()]; + let mut result = vec![]; + states[0][0] = true; + if items[0] <= capacity { states[0][items[0] as usize] = true; } + for i in 1..items.len() { + for j in 0..=capacity as usize { + if states[i-1][j] { states[i][j] = true; } + } + + for j in 0..=(capacity - items[i]) as usize { + if states[i-1][j] { states[i][j + items[i] as usize] = true; } + } + } + + let mut idx = capacity; + while idx <= capacity { + if states[items.len()-1][idx as usize] { break; } + idx += 1; + } + for i in (1..items.len()).rev() { + if idx - items[i] >= 0 && states[i-1][(idx-items[i]) as usize] { + idx -= items[i]; + result.push(items[i]); + } + } + + if idx != 0 { result.push(items[0]); } + println!("{:?}", result); + + for i in (0..=capacity as usize).rev() { + if states[items.len()-1][i] { return i as i32; } + } + 0 +} + +fn main() { + let items = vec![2, 2, 4, 6, 3]; + let capacity = 9; + let m = knapsack(items, capacity); + println!("{}", m); +} diff --git a/rust/40_dynamic_programming/knapsack.rs b/rust/40_dynamic_programming/knapsack.rs new file mode 100644 index 0000000..326c034 --- /dev/null +++ b/rust/40_dynamic_programming/knapsack.rs @@ -0,0 +1,32 @@ +fn knapsack(items: Vec<(i32, i32)>, capacity: i32) -> i32 { + let mut states = vec![-1; (capacity + 1) as usize]; + let mut result = vec![]; + states[0] = 0; + if items[0].0 <= capacity { states[items[0].0 as usize] = items[0].1; } + for i in 1..items.len() { + for j in 0..=(capacity - items[i].0) as usize { + if states[j] >= 0 { + let value = states[j] + items[i].1; + if value > states[j+items[i].0 as usize] { + states[j+items[i].0 as usize] = value; + result.push(items[i].0); + } + } + } + } + + let mut max_value = -1; + for i in (0..=capacity as usize).rev() { + if states[i] >= max_value { + max_value = states[i]; + } + } + max_value +} + +fn main() { + let items = vec![(2, 3), (2, 4), (4, 8), (6, 9), (3, 6)]; + let capacity = 9; + let m = knapsack(items, capacity); + println!("{}", m); +} diff --git a/rust/40_dynamic_programming/triangle.rs b/rust/40_dynamic_programming/triangle.rs new file mode 100644 index 0000000..096ca6f --- /dev/null +++ b/rust/40_dynamic_programming/triangle.rs @@ -0,0 +1,23 @@ +# leetcode [minimum_total](https://leetcode.com/problems/triangle/) + +pub fn minimum_total(mut triangle: Vec>) -> i32 { + if triangle.len() == 0 { return 0; } + + for i in (0..triangle.len() - 1).rev() { + for j in 0..triangle[i].len() { + triangle[i][j] = triangle[i+1][j].min(triangle[i+1][j+1]) + triangle[i][j]; + } + } + triangle[0][0] +} + +fn main() { + let triangle = vec![ + vec![2], + vec![3, 4], + vec![6, 5, 7], + vec![4, 1, 8, 3], + ]; + + println!("{:?}", minimum_total(triangle)); +}