Skip to content

Commit aa154ae

Browse files
committed
add solution of house-robber
1 parent 6c38ca5 commit aa154ae

File tree

1 file changed

+16
-0
lines changed

1 file changed

+16
-0
lines changed

house-robber/JEONGBEOMKO.java

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

0 commit comments

Comments
 (0)