-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathhouse-robber-ii.rs
58 lines (49 loc) · 1.48 KB
/
house-robber-ii.rs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
#![allow(dead_code, unused, unused_variables)]
fn main() {}
struct Solution;
impl Solution {
pub fn rob(nums: Vec<i32>) -> i32 {
let mut max = 0;
for i in 0..nums.len() {
let sum = nums[i];
let mut hash = std::collections::HashMap::new();
max = max.max(sum + Self::dp(&nums[..], i, i + 2, &mut hash));
max = max.max(sum + Self::dp(&nums[..], i, i + 3, &mut hash));
}
max
}
fn dp(
nums: &[i32],
start: usize,
now_index: usize,
hash: &mut std::collections::HashMap<usize, i32>,
) -> i32 {
let now_index = if now_index >= nums.len() {
now_index % nums.len()
} else {
now_index
};
if (start == 0 && now_index == nums.len() - 1)
|| now_index == start
|| now_index == start + 1
|| (start != 0 && now_index == start - 1)
|| (start == nums.len() - 1 && now_index == 0)
{
return 0;
}
let index1 = now_index + 2;
let v1 = if let Some(&x) = hash.get(&index1) {
x
} else {
Self::dp(nums, start, index1, hash)
};
let index2 = now_index + 3;
let v2 = if let Some(&x) = hash.get(&index2) {
x
} else {
Self::dp(nums, start, index2, hash)
};
hash.insert(now_index, nums[now_index] + v1.max(v2));
nums[now_index] + v1.max(v2)
}
}