Skip to content

Commit

Permalink
Merge pull request youngyangyang04#2120 from fwqaaq/patch-34
Browse files Browse the repository at this point in the history
Update 0213.打家劫舍II.md about rust
  • Loading branch information
youngyangyang04 authored Jun 21, 2023
2 parents aa146a5 + 477f459 commit 397b176
Showing 1 changed file with 29 additions and 0 deletions.
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

0 comments on commit 397b176

Please sign in to comment.