diff --git a/house-robber-ii/DaleSeo.rs b/house-robber-ii/DaleSeo.rs new file mode 100644 index 0000000000..9966561240 --- /dev/null +++ b/house-robber-ii/DaleSeo.rs @@ -0,0 +1,18 @@ +// TC: O(n) +// SC: O(1) +impl Solution { + pub fn rob(nums: Vec) -> i32 { + let n = nums.len(); + if n == 1 { + return nums[0]; + } + Self::rob_line(&nums[..n - 1]).max(Self::rob_line(&nums[1..])) + } + + fn rob_line(houses: &[i32]) -> i32 { + houses + .iter() + .fold((0, 0), |(prev, curr), &money| (curr, curr.max(prev + money))) + .1 + } +}