Leetcode House Robber 系列总结

来源:互联网 发布:java api 中文版 编辑:程序博客网 时间:2024/05/17 19:19

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.

题目的意思是: 如果连续抢两个挨着的房子的钱的话就会启动报警系统。用动态编程来解决,建立一个叫dp的整数数组,长度是nums的长度加一。第一项是0, 第二项是nums的第一项。i表示i位置时不相邻位置的最大和。比如说 {5, 10, 6, 7}。 dp[0] 是0,dp[1]是5 。dp[2] 应该是10, 因为10比5大。 dp[3] 应为 11,因为5 + 11 比 10 大。 所以公式是 :

Math.max(dp[i - 1], nums[i - 1] + dp[i - 2]);

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


213. House Robber II

After robbing those houses on that street, the thief has found himself a new place for his thievery so that he will not get too much attention. This time, all houses at this place are arranged in a circle. That means the first house is the neighbor of the last one. Meanwhile, the security system for these houses remain the same as for those in the previous street.

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.

题目这次变为房子是环形的。因为是环形的,最后一项等于第一项。调运一个函数,分别找到从第一项到倒数第二项、从第二项到倒数第一项的最大和。

稍微改动一下第一题找最大值的代码,使其可以用来找到一个区间的最大值。

    public int rob(int[] nums) {        if (nums.length == 1) return nums[0];        return Math.max(rob(nums, 0, nums.length - 2), rob(nums, 1, nums.length - 1));    }        private int rob(int[] nums, int low, int high) {        int prev = 0;        int curr = 0;        for (int i = low; i <= high; i++) {            int next = Math.max(prev + nums[i], curr);            prev = curr;            curr = next;        }        return curr;    }

337. House Robber III

The thief has found himself a new place for his thievery again. There is only one entrance to this area, called the "root." Besides the root, each house has one and only one parent house. After a tour, the smart thief realized that "all houses in this place forms a binary tree". It will automatically contact the police if two directly-linked houses were broken into on the same night.

Determine the maximum amount of money the thief can rob tonight without alerting the police.

Example 1:

     3    / \   2   3    \   \      3   1
Maximum amount of money the thief can rob = 3 + 3 + 1 = 7.

Example 2:

     3    / \   4   5  / \   \  1   3   1
Maximum amount of money the thief can rob = 4 + 5 = 9.


第三题直接变成了一个树。我直接引用OJ上的解法了

https://discuss.leetcode.com/topic/39834/step-by-step-tackling-of-the-problem

第一种(最慢的)

public int rob(TreeNode root) {    if (root == null) return 0;        int val = 0;        if (root.left != null) {        val += rob(root.left.left) + rob(root.left.right);    }        if (root.right != null) {        val += rob(root.right.left) + rob(root.right.right);    }        return Math.max(val + root.val, rob(root.left) + rob(root.right));}


第二种 (用一个 HashMap 来储存root 和 value, 这样就省下了查找节点的时间)
public int rob(TreeNode root) {    return robSub(root, new HashMap<>());}private int robSub(TreeNode root, Map<TreeNode, Integer> map) {    if (root == null) return 0;    if (map.containsKey(root)) return map.get(root);        int val = 0;        if (root.left != null) {        val += robSub(root.left.left, map) + robSub(root.left.right, map);    }        if (root.right != null) {        val += robSub(root.right.left, map) + robSub(root.right.right, map);    }        val = Math.max(val + root.val, robSub(root.left, map) + robSub(root.right, map));    map.put(root, val);        return val;}