#198
Medium Algorithms House robber
Array Dynamic Programming
53.0% acceptance
Jan 12, 2026
23323
508
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 the 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.
Solution
Rust
Time O(n)
Space O(1)
impl Solution {
pub fn rob(nums: Vec<i32>) -> i32 {
if nums.is_empty() {
return 0;
}
if nums.len() == 1 {
return nums[0];
}
let mut prev = 0;
let mut curr = 0;
for num in nums {
let temp = curr;
curr = curr.max(prev + num);
prev = temp;
}
curr
}
}