Skip to content

Instantly share code, notes, and snippets.

@rohanjai777
Created September 15, 2023 18:20
Show Gist options
  • Save rohanjai777/543dabe81d9646a023f78804ce0b4314 to your computer and use it in GitHub Desktop.
Save rohanjai777/543dabe81d9646a023f78804ce0b4314 to your computer and use it in GitHub Desktop.
class Solution {
public int rob(int[] nums) {
int dp[] = new int[nums.length];
Arrays.fill(dp,-1);
return maxAmount(nums,nums.length-1, dp);
}
public int maxAmount(int[] nums, int idx, int dp[]){
if(idx < 0){
return 0;
}
if(dp[idx]!=-1){
return dp[idx];
}
int max1 = maxAmount(nums,idx-2,dp) + nums[idx];
int max2 = maxAmount(nums,idx-1,dp);
return dp[idx] = Math.max(max1,max2);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment