动态规划\(▔▽▔)/
This file contains 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) { | |
// dp[i] = the maximum amount of money you can rob until index i | |
// dp[i] = Max(dp[i - 1], nums[i - 1] + dp[i - 2]) | |
if (nums == null || nums.length == 0) { | |
return 0; | |
} | |
int dp[] = new int[nums.length + 1]; | |
dp[0] = 0; | |
dp[1] = nums[0]; | |
for (int i = 2; i <= nums.length; i++) { | |
dp[i] = Math.max(dp[i - 1], nums[i - 1] + dp[i - 2]); | |
} | |
return dp[nums.length]; | |
} | |
} |
没有评论:
发表评论