lintcode-medium-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.

 

Given [3, 8, 4], return 8.

 

public class Solution {
    /**
     * @param A: An array of non-negative integers.
     * return: The maximum amount of money you can rob tonight
     */
    public long houseRobber(int[] A) {
        // write your code here
        
        if(A == null || A.length == 0)
            return (long) 0;
            
        if(A.length == 1)
            return A[0];
        if(A.length == 2)
            return Math.max(A[0], A[1]);
        
        long[] dp = new long[A.length];
        
        dp[0] = A[0];
        dp[1] = Math.max(A[0], A[1]);
        
        for(int i = 2; i < A.length; i++){
            dp[i] = Math.max(dp[i - 1], dp[i - 2] + A[i]);
        }
        
        return dp[A.length - 1];
    }
}

 

posted @ 2016-03-22 13:16  哥布林工程师  阅读(128)  评论(0编辑  收藏  举报