zoukankan      html  css  js  c++  java
  • 337. House Robber III(包含I和II)

    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(vector<int>& nums) {
            int n = nums.size();
            vector<int> f(n,0); 
            if(n <=0) return 0;
            if(n ==1) return nums[0];
            f[0] = nums[0];
            f[1] = max(nums[0],nums[1]);
            for(int i = 2 ; i< n;i++){
                f[i] = max(f[i-1],nums[i]+ f[i-2]);
            }
            return f[n-1];
        }
    };

    213. House Robber II

    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.

    还是个DP问题,因为头和尾相连,只需要在I的基础上分为有头和无头即可。

    class Solution {
    public:
        int robber(vector<int> &nums,int l,int r){
            int cur=0,pre=0;
            for(int i=l;i<=r;i++){
                int temp = max(pre + nums[i],cur);
                pre = cur;
                cur = temp;
            }
            return cur;
        }
        int rob(vector<int>& nums) {
            int n = nums.size();
            if(n <= 0) return 0;
            if(n==1) return nums[0];
            if(n==2) return max(nums[0],nums[1]);
            return max(robber(nums,0,n-2),robber(nums,1,n-1));
        }
    };

    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.

    https://leetcode.com/discuss/91899/step-by-step-tackling-of-the-problem
    非常好的进阶思路,这里贴出方法二和方法三
    方法二:

    class Solution {
    public:
        int robber(TreeNode* root,unordered_map<TreeNode*,int> &maps){
            if(!root) return 0;
            auto it = maps.find(root);
            if(it != maps.end()) return maps[root];
            int val = 0;
            if(root->left) val += robber(root->left->left,maps) + robber(root->left->right,maps);
            if(root->right) val += robber(root->right->left,maps) + robber(root->right->right,maps);
            val = max(val+root->val,robber(root->left,maps)+robber(root->right,maps));
            maps[root]= val;
            return val;
        }
        int rob(TreeNode* root) {
            unordered_map<TreeNode*,int> maps;
            return robber(root,maps);
        }
    };

    方法三:

    class Solution {
    public:
        vector<int> robber(TreeNode* root){
            if(!root) return vector<int>(2);
            vector<int> left = robber(root->left);
            vector<int> right = robber(root->right);
            std::vector<int> res(2);
            res[0] = max(left[0],left[1]) + max(right[0],right[1]);
            res[1] = root->val + left[0] + right[0];
            return res;
        }
        int rob(TreeNode* root) {
            vector<int> res = robber(root);
            return max(res[0],res[1]);      
        }
    };
  • 相关阅读:
    递归和回溯_leetcode-floodfill
    递归和回溯_leetcode131
    递归和回溯_leetcode130
    递归和回溯_leetcode93-经典的回溯题
    递归和回溯_leetcode90
    递归和回溯_leetcode79
    递归和回溯_leetcode78-经典的子集
    知识树杂谈(1)
    Android 设备兼容性(1)
    微信小程序- 生成二维码
  • 原文地址:https://www.cnblogs.com/CarryPotMan/p/5343671.html
Copyright © 2011-2022 走看看