leetcode 198.House Robber | Java最短代码实现

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.

【抛砖】

考查动态规划,基本思路是当前节点处最大值curMax = Math.max(curMax, curPrePreMax + cur)。举个例子[3, 2, 4, 7, 5, 6]

3     |                2                |            4           |         7            5           6

       |   curPrePreMax:3   |  curMax:7     |    cur:7

那么经过上述公式计算,curMax = 10:

    public int rob(int[] nums) {
        int curMax = 0, curPrePreMax = 0;
        for (int cur : nums) {
            int temp = curMax;
            curMax = Math.max(curMax, curPrePreMax + cur);
            curPrePreMax = temp;
        }
        return curMax;
    }
69 / 69  test cases passed. Runtime: 0 ms   Your runtime beats 48.10% of javasubmissions.
欢迎优化!

你可能感兴趣的:(java,LeetCode,array,dynamic,programming)