Skip to content

Commit

Permalink
Added houseRobber2 solution
Browse files Browse the repository at this point in the history
  • Loading branch information
nhistory committed Jul 5, 2024
1 parent 0d720e4 commit 93c716d
Showing 1 changed file with 20 additions and 0 deletions.
20 changes: 20 additions & 0 deletions house-robber-ii/nhistory.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
var rob = function (nums) {
// edge case
if (nums.length === 1) return nums[0];

const dp = (start, end) => {
let prev = 0,
curr = 0;
for (let i = start; i < end; i++) {
let temp = curr;
curr = Math.max(nums[i] + prev, curr);
prev = temp;
}
return curr;
};

return Math.max(dp(0, nums.length - 1), dp(1, nums.length));
};

// TC: O(n)
// SC: O(1)

0 comments on commit 93c716d

Please sign in to comment.