Skip to content
30 changes: 30 additions & 0 deletions problems/0121.买卖股票的最佳时机.md
Original file line number Diff line number Diff line change
Expand Up @@ -510,7 +510,37 @@ public class Solution
}
```

Rust:

> 贪心

```rust
impl Solution {
pub fn max_profit(prices: Vec<i32>) -> i32 {
let (mut low, mut res) = (i32::MAX, 0);
for p in prices {
low = p.min(low);
res = res.max(p - low);
}
res
}
}
```

> 动态规划

```rust
impl Solution {
pub fn max_profit(prices: Vec<i32>) -> i32 {
let mut dp = vec![-prices[0], 0];
for p in prices {
dp[0] = dp[0].max(-p);
dp[1] = dp[1].max(dp[0] + p);
}
dp[1]
}
}
```


<p align="center">
Expand Down
21 changes: 7 additions & 14 deletions problems/0122.买卖股票的最佳时机II.md
Original file line number Diff line number Diff line change
Expand Up @@ -322,13 +322,10 @@ function maxProfit(prices: number[]): number {

```Rust
impl Solution {
fn max(a: i32, b: i32) -> i32 {
if a > b { a } else { b }
}
pub fn max_profit(prices: Vec<i32>) -> i32 {
let mut result = 0;
for i in 1..prices.len() {
result += Self::max(prices[i] - prices[i - 1], 0);
result += (prices[i] - prices[i - 1]).max(0);
}
result
}
Expand All @@ -339,18 +336,14 @@ impl Solution {

```Rust
impl Solution {
fn max(a: i32, b: i32) -> i32 {
if a > b { a } else { b }
}
pub fn max_profit(prices: Vec<i32>) -> i32 {
let n = prices.len();
let mut dp = vec![vec![0; 2]; n];
dp[0][0] -= prices[0];
for i in 1..n {
dp[i][0] = Self::max(dp[i - 1][0], dp[i - 1][1] - prices[i]);
dp[i][1] = Self::max(dp[i - 1][1], dp[i - 1][0] + prices[i]);
let mut dp = vec![vec![0; 2]; prices.len()];
dp[0][0] = -prices[0];
for i in 1..prices.len() {
dp[i][0] = dp[i - 1][0].max(dp[i - 1][1] - prices[i]);
dp[i][1] = dp[i - 1][1].max(dp[i - 1][0] + prices[i]);
}
Self::max(dp[n - 1][0], dp[n - 1][1])
dp[prices.len() - 1][1]
}
}
```
Expand Down
29 changes: 29 additions & 0 deletions problems/0213.打家劫舍II.md
Original file line number Diff line number Diff line change
Expand Up @@ -301,6 +301,35 @@ function robRange(nums: number[], start: number, end: number): number {
}
```

Rust:

```rust
impl Solution {
pub fn rob(nums: Vec<i32>) -> i32 {
match nums.len() {
1 => nums[0],
_ => Self::rob_range(&nums, 0, nums.len() - 2).max(Self::rob_range(
&nums,
1,
nums.len() - 1,
)),
}
}

pub fn rob_range(nums: &Vec<i32>, start: usize, end: usize) -> i32 {
if start == end {
return nums[start];
}
let mut dp = vec![0; nums.len()];
dp[start] = nums[start];
dp[start + 1] = nums[start].max(nums[start + 1]);
for i in start + 2..=end {
dp[i] = dp[i - 1].max(dp[i - 2] + nums[i]);
}
dp[end]
}
}
```


<p align="center">
Expand Down
27 changes: 27 additions & 0 deletions problems/0337.打家劫舍III.md
Original file line number Diff line number Diff line change
Expand Up @@ -490,6 +490,33 @@ function robNode(node: TreeNode | null): MaxValueArr {
}
```

### Rust

动态规划:

```rust
use std::cell::RefCell;
use std::rc::Rc;
impl Solution {
pub fn rob(root: Option<Rc<RefCell<TreeNode>>>) -> i32 {
let (v1, v2) = Self::rob_tree(&root);
v1.max(v2)
}
pub fn rob_tree(cur: &Option<Rc<RefCell<TreeNode>>>) -> (i32, i32) {
match cur {
None => (0, 0),
Some(node) => {
let left = Self::rob_tree(&node.borrow_mut().left);
let right = Self::rob_tree(&node.borrow_mut().right);
(
left.0.max(left.1) + right.0.max(right.1), // 偷左右节点
node.borrow().val + left.0 + right.0, // 偷父节点
)
}
}
}
}
```


<p align="center">
Expand Down