From 45d824c49be7b93650916af7978804dd2673b7d3 Mon Sep 17 00:00:00 2001 From: Dale Seo <5466341+DaleSeo@users.noreply.github.com> Date: Thu, 24 Sep 2026 18:34:36 -0400 Subject: [PATCH] feat: solve house robber ii --- house-robber-ii/DaleSeo.rs | 18 ++++++++++++++++++ 1 file changed, 18 insertions(+) create mode 100644 house-robber-ii/DaleSeo.rs 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 + } +}