Merge pull request #388 from caitlingao/rust-feat-40-dynamic-programming
feat(geektime_algo): add 40 dynamic programming
This commit is contained in:
commit
871f93743c
42
rust/40_dynamic_programming/bag.rs
Normal file
42
rust/40_dynamic_programming/bag.rs
Normal file
@ -0,0 +1,42 @@
|
|||||||
|
fn knapsack(items: Vec<i32>, 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);
|
||||||
|
}
|
32
rust/40_dynamic_programming/knapsack.rs
Normal file
32
rust/40_dynamic_programming/knapsack.rs
Normal file
@ -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);
|
||||||
|
}
|
23
rust/40_dynamic_programming/triangle.rs
Normal file
23
rust/40_dynamic_programming/triangle.rs
Normal file
@ -0,0 +1,23 @@
|
|||||||
|
# leetcode [minimum_total](https://leetcode.com/problems/triangle/)
|
||||||
|
|
||||||
|
pub fn minimum_total(mut triangle: Vec<Vec<i32>>) -> 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));
|
||||||
|
}
|
Loading…
Reference in New Issue
Block a user