Created
September 15, 2023 18:20
-
-
Save rohanjai777/543dabe81d9646a023f78804ce0b4314 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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