Skip to content

Commit a5f6ade

Browse files
authored
Update Solution.rs
1 parent bc071d4 commit a5f6ade

File tree

1 file changed

+12
-4
lines changed

1 file changed

+12
-4
lines changed
Lines changed: 12 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,17 @@
11
impl Solution {
22
pub fn rob(nums: Vec<i32>) -> i32 {
3-
let mut f = [0, 0];
4-
for x in nums {
5-
f = [f[0].max(f[1]), f[0] + x];
3+
fn dfs(i: usize, nums: &Vec<i32>, f: &mut Vec<i32>) -> i32 {
4+
if i >= nums.len() {
5+
return 0;
6+
}
7+
if f[i] < 0 {
8+
f[i] = (nums[i] + dfs(i + 2, nums, f)).max(dfs(i + 1, nums, f));
9+
}
10+
f[i]
611
}
7-
f[0].max(f[1])
12+
13+
let n = nums.len();
14+
let mut f = vec![-1; n];
15+
dfs(0, &nums, &mut f)
816
}
917
}

0 commit comments

Comments
 (0)