LeetCode 動態規劃 House Robber 習題

weixin_33912445發表於2018-03-11

關於我的 Leetcode 題目解答,程式碼前往 Github:https://github.com/chenxiangcyr/leetcode-answers


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.

class Solution {
    public int rob(int[] nums) {
        // one iteration solution
        if(nums.length == 0) {
            return 0;
        }
        
        int[] profit = new int[nums.length];
        profit[0] = nums[0];
        
        for(int i = 1; i < nums.length; i++) {
            // assume rob house i
            int profit1 = nums[i] + (i > 1 ? profit[i - 2] : 0);
            // assume not rob house i
            int profit2 = profit[i - 1];
            
            profit[i] = Math.max(profit1, profit2);
        }
        
        return profit[nums.length - 1];
    }
}

LeetCode題目: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.

class Solution {
    public int rob(int[] nums) {
        // one iteration solution
        if(nums.length == 0) {
            return 0;
        }
        
        if(nums.length == 1) {
            return nums[0];
        }
        
        int profitIncludeFirstHouse = rob(nums, 0, nums.length - 2);
        int profitExcludeFirstHouse = rob(nums, 1, nums.length - 1);
        
        return Math.max(profitIncludeFirstHouse, profitExcludeFirstHouse);
    }
    
    public int rob(int[] nums, int start, int end) {
        if(start >= end) {
            return nums[start];
        }
        
        int[] profit = new int[end - start + 1];
        profit[0] = nums[start];
        
        for(int i = 1; i < profit.length; i++) {
            // assume rob house i
            int profit1 = nums[start + i] + (i > 1 ? profit[i - 2] : 0);
            // assume not rob house i
            int profit2 = profit[i - 1];
            
            profit[i] = Math.max(profit1, profit2);
        }
        
        return profit[profit.length - 1];
    }
}

LeetCode題目: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.

class Solution {
    public int rob(TreeNode root) {
        
        if(root == null) {
            return 0;
        }
        
        // If not rob root, then rob left sub tree and right sub tree
        int profit1 = rob(root.left) + rob(root.right);
        
        // If rob root
        int profit2 = root.val;
        if(root.left != null) {
            profit2 = profit2 + rob(root.left.left) + rob(root.left.right);
        }
        if(root.right != null) {
            profit2 = profit2 + rob(root.right.left) + rob(root.right.right);
        }
        
        return Math.max(profit1, profit2);
    }
}

相關文章