« Back
read.

Leetcode 198 House Robber.

Leetcode 198 House Robber

You are a professional robber planning to rob houses along a street. Each house has a certain amount of money stashed, the only constraint stopping you from robbing each of them is that adjacent houses have security system connected and it will automatically contact the police if two adjacent houses were broken into on the same night.

Given a list of non-negative integers representing the amount of money of each house, determine the maximum amount of money you can rob tonight without alerting the police.


This is a question about dynamic programming. Which we can consider it like this:

dp[i] = Math.max(dp[i-1], dp[i-2]+num[i-1])


public class Solution {
    public int rob(int[] nums) {
        if(nums.length==0){
            return 0;

        }

        if(nums==null){
            return 0;
        }
        int homenum=nums.length;
        int[] totalamount=new int[homenum+1];
        totalamount[0]=0;
        totalamount[1]=nums[0];

        for(int i=2;i < homenum+1;i++){
            totalamount[i]=Math.max(nums[i-1]+totalamount[i-2],totalamount[i-1]);
        }

        return totalamount[homenum];
    }
}


Another way to consider this is "We can use two variables, even and odd, to track the maximum value so far as iterating the array. You can use the following example to walk through the code."

public int rob(int[] num) {
    if(num==null || num.length == 0)
        return 0;

    int even = 0;
    int odd = 0;

    for (int i = 0; i < num.length; i++) {
        if (i % 2 == 0) {
            even += num[i];
            even = even > odd ? even : odd;
        } else {
            odd += num[i];
            odd = even > odd ? even : odd;
        }
    }

    return even > odd ? even : odd;
}
comments powered by Disqus