198. House Robber&213. House Robber II&337. House Robber III

来源:互联网 发布:用excel给数据取对数 编辑:程序博客网 时间:2024/06/15 01:21

LeetCode

  • 题目地址:
    • 198:https://leetcode.com/problems/house-robber/
    • 213:https://leetcode.com/problems/house-robber-ii/
    • 337:https://leetcode.com/problems/house-robber-iii/
  • 解题思路:
    • 198:动态规划,由于相邻的两家不能同时抢劫,所以判断max(抢第i家,不抢第i家)来表示当前能抢到的最大钱数
    • 213:第0家和第n-1家相邻,其余与198一样,在已经解决198的情况下,213有两种可能,一个是在0~n-2中抢到最大,另一个是在1~n-1中抢到最大,通过198的程序算出两个再比较即可
    • 337:这题思路比较多,具体可以看https://discuss.leetcode.com/topic/39834/step-by-step-tackling-of-the-problem/2,简单讲解如下,
      • 首先计算rob(root)需要额外知道6个信息,分别是rob(root->left),rob(root->right),rob(root->left->left),rob(root->left->right),rob(root->right->left),rob(root->right->right),则rob(root) = max(root->val + rob(root->left->left) + rob(root->left->right) + rob(root->right->left) + rob(root->right->right),rob(root->left) + rob(root->right))
      • 优化一:由于上列递归计算有大量重复,比如说计算rob(root)需要用到rob(root->left->left),而计算rob(root->left)也要用到rob(root->left->left),为了让rob(someTreeNode)只计算一次,用一个unordered_map来存储已经计算过的rob(someTreeNode)值,去掉大量重复的计算,C++是32ms
      • 优化二:在优化一的基础上,由于计算rob(root)仍然要用到6个信息,造成大量的递归调用,虽然说没有重复的计算,但是大量递归调用产生了大量的时间损失。提出一个新的方法返回rob(someTreeNode)的值,返回两个int,分别是抢someTreeNode结点的最大收益和不抢someTreeNode结点的最大收益,(对C++来说,不能返回数组也无法返回两个值,所以这里我用引用来返回)这样计算rob(root)只和rob(root->left)与rob(root->right)有关,大量节省了递归调用的时间,C++是9ms
  • 代码如下

    • 198:
    class Solution {public:    int rob(vector<int>& nums) {        if (nums.size() == 0) return 0;        int maxAtIndex[nums.size()+1];        maxAtIndex[nums.size()-1] = nums[nums.size()-1];        maxAtIndex[nums.size()] = 0;        for (int i = nums.size() - 2; i >= 0; i--) {            maxAtIndex[i] = max(nums[i] + maxAtIndex[i+2],maxAtIndex[i+1]);        }        return maxAtIndex[0];    }};
    • 213:
    class Solution {public:    int rob(vector<int>& nums) {        int n = nums.size();        if (n < 2) return n ? nums[0] : 0;        return max(robWithHeadAndTail(nums,0,n-2),robWithHeadAndTail(nums,1,n-1));    }    int robWithHeadAndTail(vector<int>& nums, int head, int tail) {        int pre = 0, cur = 0;        for(int i = tail; i >= head; i--) {            int tmp = max(cur,pre+nums[i]);            pre = cur;            cur = tmp;        }        return cur;    }};
    • 337:
    class Solution {public:    int rob(TreeNode* root) {        int maxWithRoot,maxWithoutRoot;        robWithTwoCondition(root,maxWithRoot,maxWithoutRoot);        return maxOf(maxWithRoot,maxWithoutRoot);    }    void robWithTwoCondition(TreeNode* root, int& withR, int& withoutR) {       if (root == NULL) {           withR = 0;           withoutR = 0;           return;       }       int leftW,leftnW,rightW,rightnW;       robWithTwoCondition(root->left, leftW, leftnW);       robWithTwoCondition(root->right, rightW, rightnW);       withR = root->val + leftnW + rightnW;       withoutR = max(leftW,leftnW) + max(rightW,rightnW);       return;    }    int maxOf(int a, int b) {        return a > b ? a : b;    }};
0 0
原创粉丝点击