Friday, June 3, 2016

House Robber I, II and III


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.

The first one is just a simple DP problem.
public int rob(int[] nums) {
        if (nums == null || nums.length == 0)
            return 0;
        int len = nums.length;
        if (len == 1)
            return nums[0];
        int[] sum = new int[len];
        sum[0] = nums[0];
        sum[1] = Math.max(nums[0], nums[1]);
        for (int i = 2; i < len; i++) {
            sum[i] = Math.max(sum[i - 1], sum[i - 2] + nums[i]);
        }
        return sum[len - 1];
    }


Note: This is an extension of House Robber.
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.

The second one, now the first house is connected with the last house, which means one of the house cannot be robbed. Then we can find the max money we can get if we rob nums[0] to nums[len - 2] and that we can get if nums[1] to nums[len - 1] are robbed.
public int rob(int[] nums) {
        if (nums == null || nums.length == 0)
            return 0;
        int len = nums.length;
        if (len == 1)
            return nums[0];
        int[] sum = new int[len];
        sum[0] = nums[0];
        sum[1] = Math.max(nums[0], nums[1]);
        return Math.max(max(nums, 0, len - 2), max(nums, 1, len - 1));
     }
     private int max(int[] nums, int start, int end) {
         if (start == end)
            return nums[start];
         int len = end - start + 1;
         int[] sum = new int[len];
         sum[0] = nums[start];
         sum[1] = Math.max(nums[start], nums[start + 1]);
         for (int i = 2; i < len; i++) {
             sum[i] = Math.max(sum[i - 1], sum[i - 2] + nums[start + i]);
         }
         return sum[len - 1];
     }


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.

The third one is a tree like structure. Consider a tree of two level, either the two leaves are robbed, or the root is robbed. Then we can recursively find the max money of robbing two leaves or root + leaves children.

public int rob(TreeNode root) { 
        return robHelper(root)[0];
        
    }
    
    private int[] robHelper(TreeNode root) {
        int[]sum = {0, 0};
        if (root == null) {
            return sum;
        }
        int[]sum_left = robHelper(root.left);
        int[]sum_right = robHelper(root.right);
        sum[1] = sum_left[0] + sum_right[0];
        sum[0] = Math.max(sum[1], sum_left[1] + sum_right[1] + root.val);
        return sum;
    }

1 comment:

  1. The development of artificial intelligence (AI) has propelled more programming architects, information scientists, and different experts to investigate the plausibility of a vocation in machine learning. Notwithstanding, a few newcomers will in general spotlight a lot on hypothesis and insufficient on commonsense application. IEEE final year projects on machine learning In case you will succeed, you have to begin building machine learning projects in the near future.

    Projects assist you with improving your applied ML skills rapidly while allowing you to investigate an intriguing point. Furthermore, you can include projects into your portfolio, making it simpler to get a vocation, discover cool profession openings, and Final Year Project Centers in Chennai even arrange a more significant compensation.


    Data analytics is the study of dissecting crude data so as to make decisions about that data. Data analytics advances and procedures are generally utilized in business ventures to empower associations to settle on progressively Python Training in Chennai educated business choices. In the present worldwide commercial center, it isn't sufficient to assemble data and do the math; you should realize how to apply that data to genuine situations such that will affect conduct. In the program you will initially gain proficiency with the specialized skills, including R and Python dialects most usually utilized in data analytics programming and usage; Python Training in Chennai at that point center around the commonsense application, in view of genuine business issues in a scope of industry segments, for example, wellbeing, promoting and account.

    ReplyDelete