-
Notifications
You must be signed in to change notification settings - Fork 4
/
main.rs
50 lines (45 loc) · 1.4 KB
/
main.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
fn main() {
assert_eq!(Solution::rob(vec![2,3,2]), 3);
}
struct Solution {}
impl Solution {
/// 1 dp * 2 times
/// equals to this question: the final result will only be one of the two following
/// situations:
/// - first is not selected, then freely select in the following
/// - last is not selected, then freely select in the following
pub fn rob(mut nums: Vec<i32>) -> i32 {
if nums.len() < 1 { return 0 }
if nums.len() == 1 { return nums[0] }
if nums.len() == 2 { return std::cmp::max(nums[0], nums[1]) }
let mut dp = vec![0; nums.len()];
let last = nums.pop().unwrap();
dp[0] = nums[0];
dp[1] = std::cmp::max(nums[0], nums[1]);
for i in 2..nums.len() {
dp[i] = std::cmp::max(dp[i-1], dp[i-2] + nums[i]);
}
let res = dp[nums.len() - 1];
nums.push(last);
nums.remove(0);
dp[0] = nums[0];
dp[1] = std::cmp::max(nums[0], nums[1]);
for i in 2..nums.len() {
dp[i] = std::cmp::max(dp[i-1], dp[i-2] + nums[i]);
}
std::cmp::max(res, dp[nums.len() - 1])
}
}
#[cfg(test)]
mod test {
use crate::*;
#[test]
fn basic() {
assert_eq!(Solution::rob(vec![2,3,2]), 3);
assert_eq!(Solution::rob(vec![1,2,3,1]), 4);
}
#[test]
fn fail() {
assert_eq!(Solution::rob(vec![1,2,1,1]), 3);
}
}