Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit 87fa0ee

Browse files
committed
File(s) Modified: 0198-houses-robber.java
Language(s) Used: java Submission URL: https://leetcode.com/problems/house-robber/submissions/1030302644/
1 parent c6bda97 commit 87fa0ee

File tree

1 file changed

+14
-0
lines changed

1 file changed

+14
-0
lines changed

java/0198-house-robber.java

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -32,4 +32,18 @@ public int robDP(int[] nums) {
3232
}
3333
return dp[nums.length - 1];
3434
}
35+
36+
// DP with O(1) space
37+
public int robDP2(int[] nums) {
38+
if (nums == null || nums.length == 0) return 0;
39+
40+
int dp0 = 0, dp1 = 0, curr;
41+
42+
for (int i = 0; i < nums.length; i++) {
43+
curr = Math.max(dp0 + nums[i], dp1);
44+
dp0 = dp1;
45+
dp1 = curr;
46+
}
47+
return Math.max(dp0, dp1);
48+
}
3549
}

0 commit comments

Comments
 (0)