Skip to content

Commit dc07507

Browse files
committed
solve: Week 01 house robber
1 parent 2577233 commit dc07507

File tree

1 file changed

+16
-0
lines changed

1 file changed

+16
-0
lines changed

house-robber/eunice-hong.ts

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
/**
2+
* Finds the maximum amount of money that can be robbed without robbing two adjacent houses.
3+
* Uses dynamic programming to track the best outcome at each step.
4+
*
5+
* @param {number[]} nums
6+
* @return {number}
7+
*/
8+
function rob(nums: number[]): number {
9+
const dp = new Array(nums.length + 1);
10+
dp[0] = 0;
11+
dp[1] = nums[0];
12+
for (let i = 2; i < dp.length; i++) {
13+
dp[i] = Math.max(dp[i - 1], dp[i - 2] + nums[i - 1]);
14+
}
15+
return dp[dp.length - 1];
16+
};

0 commit comments

Comments
 (0)