We read every piece of feedback, and take your input very seriously.
To see all available qualifiers, see our documentation.
There was an error while loading. Please reload this page.
1 parent 893534d commit 6236e7fCopy full SHA for 6236e7f
Dynamic Programming/HouseRobber.java
@@ -0,0 +1,29 @@
1
+package DP;
2
+
3
+// https://leetcode.com/problems/house-robber/
4
+public class HouseRobber {
5
+ public int rob(int[] nums) {
6
+ if(nums.length == 0) {
7
+ return 0;
8
+ }
9
10
+ if(nums.length == 1) {
11
+ return nums[0];
12
13
14
+ if(nums.length == 2){
15
+ Math.max(nums[0],nums[1]);
16
17
18
+ int n = nums.length;
19
+ int dp[] = new int[n];
20
+ dp[0] = nums[0];
21
+ dp[1] = Math.max(nums[0], nums[1]);
22
23
+ for(int i=2; i<n; i++){
24
+ dp[i] = Math.max(dp[i-1], (dp[i-2]+nums[i]));
25
26
27
+ return dp[n-1];
28
29
+}
0 commit comments