Created
April 1, 2023 08:14
-
-
Save vamsitallapudi/039130cee3da277dee0e574fa75e15a0 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 n = nums.length; | |
int[] dp = new int[n]; | |
dp[0] = nums[0]; | |
for(int i = 1; i< n;i++) { | |
int pick = nums[i]; | |
if(i>1) pick += dp[i-2]; | |
int nonPick = dp[i-1]; | |
dp[i] = Math.max(pick, nonPick); | |
} | |
return dp[n-1]; | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment