代码随想录算法训练营第五十天|198.打家劫舍、213.打家劫舍II、 337.打家劫舍III

代码随想录算法训练营第五十天|198.打家劫舍、213.打家劫舍II、 337.打家劫舍III

  • 198.打家劫舍
  • 213.打家劫舍II
  • 337.打家劫舍III

感觉还好

198.打家劫舍

题目链接:198.打家劫舍
文章链接
状态:挺简单。

代码

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

213.打家劫舍II

题目链接:213.打家劫舍II
文章链接
状态:还行

代码

class Solution {
public:
    int rob(vector<int>& nums) {
        if (nums.size() == 0) return 0;
        if (nums.size() == 1) return nums[0];
        int result1 = robRange(nums, 0, nums.size() - 2); 
        int result2 = robRange(nums, 1, nums.size() - 1); 
        return max(result1, result2);
    }
    
    int robRange(vector<int>& nums, int start, int end) {
        if (end == start) return nums[start];
        vector<int> dp(nums.size());
        dp[start] = nums[start];
        dp[start + 1] = max(nums[start], nums[start + 1]);
        for (int i = start + 2; i <= end; i++) {
            dp[i] = max(dp[i - 2] + nums[i], dp[i - 1]);
        }
        return dp[end];
    }
};

337.打家劫舍III

题目链接:337.打家劫舍III
文章链接
状态:二叉树学的没那么好

代码

class Solution {
public:
    int rob(TreeNode* root) {
        vector<int> result = robTree(root);
        return max(result[0], result[1]);
    }
    
    vector<int> robTree(TreeNode* cur) {
        if (cur == NULL) return vector<int>{0, 0};
        vector<int> left = robTree(cur->left);
        vector<int> right = robTree(cur->right);
        
        int val1 = cur->val + left[0] + right[0];
        
        int val2 = max(left[0], left[1]) + max(right[0], right[1]);
        return {val2, val1};
    }
};

你可能感兴趣的:(算法)