Skip to content

Commit

Permalink
Update 198_House_Robber.java
Browse files Browse the repository at this point in the history
  • Loading branch information
seanprashad committed Apr 22, 2021
1 parent c4c7f38 commit c648044
Showing 1 changed file with 6 additions and 16 deletions.
22 changes: 6 additions & 16 deletions Dynamic Programming/198_House_Robber.java
Original file line number Diff line number Diff line change
@@ -1,23 +1,13 @@
class Solution {
public int rob(int[] nums) {
if (nums == null || nums.length == 0) {
return 0;
}
if (nums.length == 1) {
return nums[0];
}
if (nums.length == 2) {
return Math.max(nums[0], nums[1]);
}

int prevHouse = 0, prevTwoHouses = 0, currHouse = 0;
int[] dp = new int[nums.length + 1];
dp[0] = 0;
dp[1] = nums[0];

for (int i = 0; i < nums.length; i++) {
currHouse = Math.max(prevHouse, prevTwoHouses + nums[i]);
prevTwoHouses = prevHouse;
prevHouse = currHouse;
for (int i = 1; i < nums.length; i++) {
dp[i + 1] = Math.max(dp[i], dp[i - 1] + nums[i]);
}

return currHouse;
return dp[nums.length];
}
}

0 comments on commit c648044

Please sign in to comment.