Leetcode191-House Robbery

Problem Description

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 a thief at 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 that can be stolen in one night without touching the alarm device.
Example 1:

输入:[1,2,3,1]
输出:4
解释:偷窃 1 号房屋 (金额 = 1) ,然后偷窃 3 号房屋 (金额 = 3)。
     偷窃到的最高金额 = 1 + 3 = 4

Example 2:

输入:[2,7,9,3,1]
输出:12
解释:偷窃 1 号房屋 (金额 = 2), 偷窃 3 号房屋 (金额 = 9),接着偷窃 5 号房屋 (金额 = 1)。
     偷窃到的最高金额 = 2 + 9 + 1 = 12

Problem-solving ideas

From the official problem solution of Likou:

Consider the simplest case first. If there is only one house, steal that house up to the maximum total amount. If there are only two houses, because the two houses are adjacent to each other and cannot be stolen at the same time, only one of the houses can be stolen. Therefore, the house with the higher amount of money can be selected for theft, and the maximum total amount can be stolen.
If the number of houses is more than two, how should the maximum total amount that can be stolen be calculated? For the kth (k>2) house, there are two options:

  • Steal the kth house, then the k-1th house cannot be stolen. The total amount stolen is the sum of the highest sum of the first k-2 houses and the kth house.

  • The kth house is not stolen, and the total amount stolen is the highest total amount of the previous k−1 houses.
    Choose the option with the larger total amount of theft from the two options. The total amount of theft corresponding to this option is the highest total amount that can be stolen from the previous kk houses.

Use dp[i] to denote the highest total amount that can be stolen from the previous i houses, then there is the following state transition equation:

dp[i]=max(dp[i−2]+nums[i],dp[i−1])

The boundary conditions are:

dp[0]=nums[0];						//只有一间房屋,则偷窃该房屋 
dp[1]=max(nums[0],nums[1])			//只有两间房屋,选择其中金额较高的房屋进行偷窃

The final answer is dp[n-1], where nn is the length of the array.

Code

class Solution {
    
    
    public int rob(int[] nums) {
    
    
        //如果没有房屋,直接返回0
        if(nums.length==0){
    
    
            return 0;
        }
        //如果只有1个房屋,返回这个房屋的金额
        if(nums.length==1){
    
    
            return nums[0];
        }
        int n=nums.length;
        //用 dp[i]表示前 i 间房屋能偷窃到的最高总金额
        int dp[]=new int[n];
        //只有1个房屋时,能偷窃到的最高总金额就是该房屋的金额
        dp[0]=nums[0];
        //有2个房屋时,能偷窃到的最高总金额两个房屋金额的最大值
        dp[1]=Math.max(nums[0],nums[1]);
        for(int i=2;i<n;i++){
    
    
            //状态转移方程表示偷窃第k间房屋和不偷窃第k间房屋的较大值
            dp[i]=Math.max(dp[i-1],dp[i-2]+nums[i]);
        }
        return dp[n-1];
    }
}

Insert picture description here

Guess you like

Origin blog.csdn.net/qq_39736597/article/details/114279856