-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathhouse_robber.rs
52 lines (45 loc) · 1.41 KB
/
house_robber.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
/// You are a professional robber planning to rob houses along a street. Each
/// house has a certain amount of money stashed, the only constraint stopping
/// you from robbing each of them is that adjacent houses have security systems
/// connected and it will automatically contact police if two adjacent houses
/// were broken into on the same night.
///
/// Given an integer array `nums` representing the amount of money of each
/// house, return the maximum amount of money you can rob tonight without
/// alerting the police.
struct Solution;
impl Solution {
// Switched to bottom-up iterative solution
pub fn rob(nums: Vec<i32>) -> i32 {
let n = nums.len();
if n == 1 {
nums[0]
} else {
let mut most = vec![0; n];
most[0] = nums[0];
most[1] = nums[0].max(nums[1]);
for i in 2..n {
let sum_with = nums[i] + most[i-2];
let sum_without = most[i-1];
most[i] = sum_with.max(sum_without);
}
most[n-1]
}
}
}
#[cfg(test)]
mod tests {
use super::Solution;
#[test]
fn example_1() {
let nums = vec![1, 2, 3, 1];
let result = Solution::rob(nums);
assert_eq!(result, 4);
}
#[test]
fn example_2() {
let nums = vec![2, 7, 9, 3, 1];
let result = Solution::rob(nums);
assert_eq!(result, 12);
}
}