Day48|leetcode 198. Raid house, 213. Raid house II, Raid house|||

leetcode 198. Robbery

Topic link: 198. Robbery - LeetCode

Video link: Dynamic programming, should I steal this room? | LeetCode: 198. Robbing Houses_哔哩哔哩_bilibili

topic overview

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  .

Example 1:

Input: [1,2,3,1]
 Output: 4
 Explanation: Steal house 1 (amount = 1), then steal house 3 (amount = 3).
     Maximum amount stolen = 1 + 3 = 4.

Example 2:

Input: [2,7,9,3,1]
 Output: 12
 Explanation: Steal house 1 (money = 2), steal house 3 (money = 9), then steal house 5 (money = 1).
     Maximum amount stolen = 2 + 9 + 1 = 12.

train of thought

This question only says that two adjacent rooms cannot be stolen, so as long as it is not two adjacent rooms, it doesn’t matter which room you choose, as long as you steal the most money, there are not many limiting factors.

It's still a five-part series

1. Determine the meaning of the dp array:

dp[i]: Considering the houses within the subscript i (including i), the maximum amount that can be stolen is dp[i].

2. Determine the recursive formula:

偷i:dp[i]=nums[i]+dp[i-2]

不偷i:dp[i]=dp[i-1]

So the recursive formula: dp[i]=max(nums[i]+dp[i-2], dp[i-1]).

3. Array initialization:

dp[0]=nums[0]

dp[1]=max(nums[0],nums[1])

4. Determine the traversal order:

front to back

5. Print the dp array:

198. Robbery

 

Code 

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

    }
};

leetcode 213. Hit House II

Topic link: 213. Robbery II - LeetCode

Video link: Dynamic programming, if the rooms are connected into a ring, can you still steal it? | LeetCode: 213. Robbery II_哔哩哔哩_bilibili

topic overview

You are a professional thief planning to rob houses along the street, each of which has a certain amount of cash hidden in it. All the houses in this place  are arranged in a circle  , which means that the first house and the last house are right next to each other. At the same time, adjacent houses are equipped with interconnected anti-theft systems. If two adjacent houses are broken into by thieves at the same night, the system will automatically call the police  .

Given an array of non-negative integers representing the amount of money stored in each house, calculate the   maximum amount you can steal tonight without setting off the alarm .

Example 1:

Input: nums = [2,3,2]
 Output: 3
 Explanation: You cannot first steal house 1 (amount = 2) and then steal house 3 (amount = 2), because they are adjacent.

Example 2:

Input: nums = [1,2,3,1]
 Output: 4
 Explanation: You can first steal house 1 (amount = 1) and then steal house 3 (amount = 3).
     Maximum amount stolen = 1 + 3 = 4.

Example 3:

Input: nums = [1,2,3]
 Output: 3

train of thought

In fact, this question is similar to the previous question, that is, this question has become a loop, which is not conducive to thinking. However, we can split the ring into a linear array, then there will be three situations: (consideration is just consideration, not necessarily stealing)

1. Consider not including the first and last elements, as shown in the figure:

213. Dajia Palace II

2. Consider including the first element, as shown in the figure:

213. Robbery II1

3. Consider including tail elements, as shown in the figure:

213. House Robbery II 2

 The second and third cases include the first case, so this question just intercepts the array and passes it into the main function to get the maximum value.

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);
    }
    // 198.打家劫舍的逻辑
    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];
    }
};

leetcode 337. Robbery III

Topic link: 337. Robbery III - LeetCode

Video link: Dynamic programming, the room is connected into a tree, should I steal it or not? | LeetCode: 337. Robbery 3_哔哩哔哩_bilibili

topic overview

Thieves have discovered a new area to steal from. There is only one entrance to this area, which we call  root。

Except rootfor , each house has one and only one "parent" house connected to it. After some reconnaissance, the clever thief realized that "all the houses in this place are arranged like a binary tree". If two directly connected houses are robbed on the same night, the houses will automatically call the police.

The maximum amount a thief can steal without setting off an alarm  .

 

Example 1:

Input: root = [3,2,3,null,3,null,1]
 Output: 7 
 Explanation:  The maximum amount a thief can steal in one night 3 + 3 + 1 = 7

Example 2:

Input: root = [3,4,5,1,3,null,1]
 Output: 9
 Explanation:  The maximum amount a thief can steal in one night 4 + 5 = 9

train of thought

This question can be regarded as an introductory question for tree-shaped dp. In terms of the recursive trilogy and the five-part dynamic regulation:

1. Determine the parameters and return value of the recursive function

Return value: an array of length 2.

Determine the meaning of the dp array: a subscript of 0 records the maximum money obtained by not stealing the node, and a subscript of 1 records the maximum money obtained by stealing the node.

2. Determine the termination condition

Return when empty.

3. Determine the traversal order

post order traversal

4. Determine the logic of single-level recursion

Current node steal: val1 = cur->val + left[0] + right[0]

The current node does not steal: val2 = max(left[0], left[1]) + max(right[0], right[1])

5. Example derivation dp array

 

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/m0_74583479/article/details/132537449