Update 背包理论基础01背包-2.md about rust

This commit is contained in:
fwqaaq
2023-04-28 18:50:56 +08:00
committed by GitHub
parent f97940758e
commit e6ad637e64

View File

@ -406,6 +406,34 @@ object Solution {
}
```
### Rust
```rust
pub struct Solution;
impl Solution {
pub fn wei_bag_problem2(weight: Vec<usize>, value: Vec<usize>, bag_size: usize) -> usize {
let mut dp = vec![0; bag_size + 1];
for i in 0..weight.len() {
for j in (weight[i]..=bag_size).rev() {
if j >= weight[i] {
dp[j] = dp[j].max(dp[j - weight[i]] + value[i]);
}
}
}
dp[dp.len() - 1]
}
}
#[test]
fn test_wei_bag_problem2() {
println!(
"{}",
Solution::wei_bag_problem1(vec![1, 3, 4], vec![15, 20, 30], 4)
);
}
```
<p align="center">
<a href="https://programmercarl.com/other/kstar.html" target="_blank">
<img src="../pics/网站星球宣传海报.jpg" width="1000"/>