2019年1月8日星期二

[LeetCode] 198. House Robber

https://leetcode.com/problems/house-robber/

动态规划\(▔▽▔)/ 


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];
}
}
view raw rob.java hosted with ❤ by GitHub

没有评论:

发表评论