Skip to content

Commit 115c0e3

Browse files
committed
feat: house-robber solution
1 parent b249de9 commit 115c0e3

File tree

1 file changed

+14
-0
lines changed

1 file changed

+14
-0
lines changed

house-robber/sm9171.java

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
class Solution {
2+
public int rob(int[] nums) {
3+
if (nums.length == 0) return 0;
4+
if (nums.length == 1) return nums[0];
5+
6+
nums[1] = Math.max(nums[0], nums[1]);
7+
8+
for (int i = 2; i < nums.length; i++) {
9+
nums[i] = Math.max(nums[i - 1], nums[i - 2] + nums[i]);
10+
}
11+
12+
return nums[nums.length - 1];
13+
}
14+
}

0 commit comments

Comments
 (0)