LeetCode-- loot problem

Q: You are a professional thief to steal street plan of the house. Each room are in possession of some cash, the only constraints affecting steal your neighbor's house is equipped with anti-theft system communicate with each other, if two adjacent houses on the same night, thieves broke into the system will automatically alarm .
Given a representative from each non-negative integer array of Housing storage amount calculated in case you do not touch the alarm device, can steal the maximum amount to.

Example 1:
Input: [1,2,3,1]
Output: 4
Explanation: Housing theft No. 1 (amount = 1), then the theft Housing 3 (amount = 3).
  The maximum amount to theft = 3 + 1 = 4.

Example 2:
Input: [2,7,9,3,1]
Output: 12
Explanation: Housing theft No. 1 (amount = 2), 3 theft Housing (amount = 9), followed by theft Housing 5 (1 = Amount ).
  The maximum amount to theft = 2 + 9 + 1 = 12.

A: A typical dynamic programming problem.
The current house is divided into two possibilities, either grab or not grab.
If the current robbed, either did not grab the last time, either on the last robbed;
if the current did not grab, grab either the last or the last time is robbed;
so, dp on the good writing:

dp [i] [0] = Math.max (dp [i - 1] [1], dp [i - 2] [1]); // grab either the last or the second last grab is
dp [i] [1] = Math.max (dp [i - 1] [0] + nums [i], dp [i - 2] [1] + nums [i]); // last either no rush either on the last robbed

Code:

    public static int rob(int[] nums) {
        if (nums.length == 0)
            return 0;
        if(nums.length == 1)
            return nums[0];
        int[][] dp = new int[nums.length][2];
        dp[0][0] = 0;
        dp[0][1] = nums[0];
        dp[1][0] = nums[0];
        dp[1][1] = nums[1];
        for (int i = 2; i < nums.length; i++) {
            dp[i][0] = Math.max(dp[i - 1][1], dp[i - 2][1]);
            dp[i][1] = Math.max(dp[i - 1][0] + nums[i], dp[i - 2][1] + nums[i]);
        }
        return Math.max(dp[nums.length - 1][0], dp[nums.length - 1][1]);
    }

Or, come from the tail head :( Quote from "labuladong algorithm Cheat Sheet")

// 返回 nums[start..] 能抢到的最⼤值
private int dp(int[] nums, int start) {
    if (start >= nums.length) {
    return 0;
    }
    int res = Math.max(
        // 不抢,去下家
        dp(nums, start + 1),
        // 抢,去下下家
        nums[start] + dp(nums, start + 2)
    );
    return res;
}

This memo may be added to prevent double counting.
This is a top-down, bottom-up may also reduce the computational

int rob(int[] nums) {
    int n = nums.length;
    // dp[i] = x 表⽰:
    // 从第 i 间房⼦开始抢劫,最多能抢到的钱为 x
    // base case: dp[n] = 0
    int[] dp = new int[n + 2];
    for (int i = n - 1; i >= 0; i--) {
        dp[i] = Math.max(dp[i + 1], nums[i] + dp[i + 2]);
    }
    return dp[0];
    }

Since only and dp [i + 1], dp [i + 2] Related, two variables can only record it.

Q: You are a professional thief to steal street plan of the house, each room are in possession of some cash. This place all the houses are in a circle , which means that the first and the last house is next to the house. Meanwhile, neighboring houses equipped with anti-theft system communicate with each other, if two adjacent houses on the same night, thieves broke into the system will automatically alarm.
Given a representative from each non-negative integer array of Housing storage amount calculated in case you do not touch the alarm device, can steal the maximum amount to.

Example 1:
Input: [2,3,2]
Output: 3
Explanation: You can not steal first No. 1 Housing (amount = 2), and then steal the 3rd house (money = 2), because they are adjacent.

Example 2:
Input: [1,2,3,1]
Output: 4
Explanation: You can first steal Housing 1 (amount = 1), then the theft Housing 3 (amount = 3).
  The maximum amount to theft = 3 + 1 = 4.

A:
therefore especially first, the end of the room can not therefore especially robbed, then there can be only three different situations: either not robbed; or the first frame submenus do not grab between rooms was robbed last frame; submenus room was robbed between either of the last frame between ⼀ not rush.

It is simple, ah, these three cases, the kind of result is largest, is the final answer chant! But, in fact, we do not need ⽐ than three cases, and as long as ⽐ than the case of a three situations ⼆ on ⾏, because in both cases the situation room for choice ⽐ submenus of ⼀ zoomed Yeah, count the money comes in handy room submenus are ⾥ negative, so the choice zoomed, certainly not the result of optimal decision ⼩. Brought above it.

    public static int rob(int[] nums) {
        int n = nums.length;
        if (n == 0)
            return 0;
        if (n == 1)
            return nums[0];
        return Math.max(maxRob(nums, 0, n - 2), maxRob(nums, 1, n - 1));
    }

    private static int maxRob(int[] nums, int start, int end) {
        int dp_2 = 0;
        int dp_1 = 0;
        int dp = 0;
        for (int i = end; i >= start; i--) {
            dp = Math.max(dp_1, dp_2 + nums[i]);
            dp_2 = dp_1;
            dp_1 = dp;
        }
        return dp;
    }

Q: After the completion of the last lap after robbing a street and house, discovered a thief can steal new areas. The region has only one entrance, which we call the "root." In addition to the "root", and only the houses a "parent" house associated therewith. After some reconnaissance, clever thief realized that "this place all the houses are arranged similar to a binary tree." If both houses are directly connected robbed the same night, the house alarm.
Calculated without touching the alarm, a thief can steal the maximum amount of night.

Example 1:
Input: [3,2,3, null, 3, null, 1]
3
/
2 3
\ \
3 1
Output: 7
explains: a thief to steal the night maximum amount can be 3 + 3 = 7 + 1 = .

Example 2:
Input: [3,4,5,1,3, null,. 1]
  . 3
/
4 5
/ \ \
. 1. 1. 3
Output: 9
Explanation: a thief to steal the night can be the maximum amount = 5 + 4 = 9 .

A:
I see the tree, recursively.

    Map<TreeNode, Integer> map = new HashMap<>();

    public int rob(TreeNode root) {
        if (root == null)
            return 0;
        if (map.containsKey(root))
            return map.get(root);
        int do_it = root.val + (root.left == null ? 0 : rob(root.left.left) + rob(root.left.right)) + (root.right == null ? 0 : rob(root.right.left) + rob(root.right.right));
        int not_do = rob(root.left) + rob(root.right);
        int res = Math.max(do_it, not_do);
        map.put(root, res);
        return res;
    }

Guess you like

Origin www.cnblogs.com/xym4869/p/12591698.html