leetCode 198. Robbery Dynamic Programming

198. Robbery - LeetCode

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 restriction 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 alarm .

Given an array of non-negative integers representing the amount of money stored in each house, calculate the maximum amount of money you can steal in one night without triggering the alarm device  . 

Example 1:

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

Example 2:

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

>>Ideas and analysis

  • Common confusion (•_•)?: Should I steal or not steal in the current situation?
  • Solve the confusion: whether the current house is stolen or not depends on whether the previous house and the previous two houses were stolen~
  • The current state and the previous state have a dependency relationship, and the recursive formula of the dynamic rule can be used

>>Five Steps of Dynamic Rules

1. Determine the meaning of dp array (dp table) and subscripts

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

2. Determine the recursion formula

The factor that determines dp[i] is whether to steal the i-th room  or not.

① Consider the situation of stealing

  • If the i-th room is stolen, then dp[i] = dp[i - 2] + nums[i] , that is: the i-1th room must not be considered , find the subscript i - 2 (including i - For houses within 2) , the maximum amount that can be stolen is dp[i - 2] plus the money stolen from the i-th room.

 ② Consider not stealing

  • If you do not steal the i-th room, then dp[i] = dp[i - 1] , that is, consider the i - 1 room (note that it is considered here , not necessarily to steal i - 1 )

dp[i] takes the maximum value of these two situations, that is, dp[i] = max(dp[i - 2] + nums[i],dp[i - 1]);

3.dp array initialization

From the recursive formula dp[i] = max(dp[i - 2] + nums[i],dp[i - 1]); we can see that the basis of the recursive formula is dp[0] and dp[1] .

  • dp[0] = nums[0];
  • dp[1] = max(nums[0],nums[1]);
vector<int> dp(nums.size());
dp[0] = nums[0];
dp[1] = max(nums[0], nums[1]);

4. Determine the traversal order

dp[i] is derived based on dp[i - 2] and dp[i - 1] , so it must be traversed from front to back! ! !

for (int i = 2; i < nums.size(); i++) {
    dp[i] = max(dp[i - 2] + nums[i], dp[i - 1]);
}

5. Use an example to derive the dp array

Enter [2,7,9,3,1] and [2,7,9,6,1]

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

// 时间复杂度: O(n)
// 空间复杂度: O(n)
  • Time complexity: O(n)
  • Space complexity: O(n)

>>Reference and recommended articles and videos 

Code Random Record (programmercarl.com)

Dynamic programming, should I steal this room? | LeetCode: 198. Robbery_bilibili_bilibili

Robbery is a classic problem solved by DP. This question is also an entry-level question for robbing a house. We will also use variant methods to rob later.

Class screenshots from Code Caprice:

Guess you like

Origin blog.csdn.net/weixin_41987016/article/details/133390944