Skip to content

Commit fa180cf

Browse files
authored
Create 198. House Robber
1 parent 443044a commit fa180cf

File tree

1 file changed

+14
-0
lines changed

1 file changed

+14
-0
lines changed

198. House Robber

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+
int[] dp = new int[nums.length];
4+
dp[0]=nums[0];
5+
if(nums.length==1) return nums[0];
6+
if(nums.length==2) return Math.max(nums[0],nums[1]);
7+
dp[1] = Math.max(nums[0],nums[1]);
8+
9+
for(int index=2;index<nums.length;index++){
10+
dp[index] = Math.max(dp[index-2]+nums[index],dp[index-1]);
11+
}
12+
return dp[nums.length-1];
13+
}
14+
}

0 commit comments

Comments
 (0)