Skip to content

Commit 2779395

Browse files
Sean PrashadSean Prashad
authored andcommitted
Update 198_House_Robber.java
1 parent 8263723 commit 2779395

File tree

1 file changed

+6
-6
lines changed

1 file changed

+6
-6
lines changed

Dynamic Programming/198_House_Robber.java

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -10,14 +10,14 @@ public int rob(int[] nums) {
1010
return Math.max(nums[0], nums[1]);
1111
}
1212

13-
int[] dp = new int[nums.length];
14-
dp[0] = nums[0];
15-
dp[1] = Math.max(nums[0], nums[1]);
13+
int prevHouse = 0, prevTwoHouses = 0, currHouse = 0;
1614

17-
for (int i = 2; i < nums.length; i++) {
18-
dp[i] = Math.max(dp[i - 1], dp[i - 2] + nums[i]);
15+
for (int i = 0; i < nums.length; i++) {
16+
currHouse = Math.max(prevHouse, prevTwoHouses + nums[i]);
17+
prevTwoHouses = prevHouse;
18+
prevHouse = currHouse;
1919
}
2020

21-
return dp[nums.length - 1];
21+
return currHouse;
2222
}
2323
}

0 commit comments

Comments
 (0)