Algorithm Learning|Dynamic Programming LeetCode 198. Robbery, 213. Robbery II, 337. Robbery III

1. Robbery

You are a professional thief planning to steal houses along the street. There is a certain amount of cash hidden in each room. The only restrictive factor that affects your theft is that the adjacent houses are equipped with interconnected anti-theft systems. If two adjacent houses are broken into by thieves on the same night, the system will automatically call the police. .
Given an array of non-negative integers representing the amount stored in each house, calculate the maximum amount you can steal in one night without triggering the alarm.

train of thought

1. dp[i]: consider the largest gold coin dp[i] stolen by subscript i (including i)
2. Recursive formula: steal i but not steal i dp[i] = max(dp[i - 2] + nums [i], dp[i - 1])
3. Initialization: dp[0] = nums[0] dp[1] = max(nums[0],nums[1])
4. Traversal order: from front to back

Implementation code

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];
    }
};

2. Robbery||

Ring room, adjacent rooms cannot be stolen

train of thought

Three cases of ring:
1. Consider not including the first element
2. Consider including the first element, not including the tail element
3. Consider including the tail element, not including the first element

Implementation code

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];
    }
};

3. Robbery|||

All the houses in this place are arranged like a "binary tree". If two directly connected houses are robbed on the same night, the house will automatically call the police. Calculate the maximum amount that the thief can steal in one night without triggering the alarm. amount.

train of thought

dp[0] the maximum money obtained by not stealing the current node dp[1] the maximum money obtained by stealing the current node
post-order traversal, from bottom to top, record the status of each node stealing and not stealing, layer by layer , concentrate the optimal solution on the root node

Implementation code

class Solution {
    
    
public:
    int rob(TreeNode* root) {
    
    
        vector<int> result = robTree(root);
        return max(result[0], result[1]);
    }
    //长度为2的数组,0:不偷 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); // 右
        // 偷cur,那就不能偷左右孩子
        int val1 = cur->val + left[0] + right[0]; //中
        // 不偷cur,那么可以偷也可以不偷左右结点,则取较大的情况
        int val2 = max(left[0], left[1]) + max(right[0], right[1]);
        return {
    
    val2,val1};
    }
};

Guess you like

Origin blog.csdn.net/li26324949/article/details/129962375