leetcode 198. House Robber

本文详细解析了“打家劫舍”问题的三种算法解决方案,包括自顶向下记忆化搜索、动态规划自下向上两种不同方向的考虑。通过具体代码示例,深入探讨了如何在不触动相邻房屋警报的情况下,实现最大金额的盗窃。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

题目:You are a professional robber planning to rob houses along a street. Each house has a certain amount of money stashed, the only constraint stopping you from robbing each of them is that adjacent houses have security system connected and it will automatically contact the police if two adjacent houses were broken into on the same night.

Given a list of non-negative integers representing the amount of money of each house, determine the maximum amount of money you can rob tonight without alerting the police.

Input: [1,2,3,1]
Output: 4
Explanation: Rob house 1 (money = 1) and then rob house 3 (money = 3).
Total amount you can rob = 1 + 3 = 4.

大概意思:相邻房间不能偷取,求偷取的最大值。

算法思路1(自顶向下记忆化搜索);函数maxRob定义为:考虑偷取 [x…len-1]范围的房子的最大值,依次遍历index到 len-1下标对应的值,比较当前max值和nums[i]+maxRob(i+2…len-1),取最大值更新max,用记忆数组来保存max,最后返回max。

参考代码:

//leetcode 198. House Robber  自顶向下 记忆化搜索
	static int len;
    public static int rob(int[] nums) {
    	len = nums.length;
    	if(len==0)
    		return 0;
    	int[] memo = new int[len];
    	for(int i=0;i<len;i++)
    		memo[i] = -1;
		return maxRob(nums,0,memo);     
    }
    
    public static int maxRob(int[] nums,int index,int[] memo){  
    	if(index == len-1){
    		//System.out.println("memo["+index+"]="+memo[index]);
    		return nums[index];    		
    	}
    	if(index >= len)
    		return 0;
    	if(memo[index] != -1)
    		return memo[index];
    	int max=0;
    	for(int i=index;i<len;i++){
    		//System.out.println("i="+i+" nums[i]="+nums[i]+" max="+max);
    		max = Math.max(max, nums[i] + maxRob(nums, i+2,memo));
    		//System.out.println("i="+i+" nums[i]="+nums[i]+" max="+max);
    	}
    	memo[index] = max;
		return max;
    	
    }

算法思路2(动态规划):函数定义:考虑偷取 [x…n-1]范围的房子,用一个数组memo保存子问题的解,初始化memo[len-1] = nums[len-1],x从n-1到0依次计算其最大值,并保存至memo数组中。最终返回memo[0]。

参考代码:

//leetcode 198. House Robber  动态规划  自下向上 考虑偷取 [x...n-1]范围的房子 (函数的定义)
	public static int rob2(int[] nums){
		int len = nums.length;
		if(len==0)
			return 0;
		int[] memo = new int[len];
		memo[len-1] = nums[len-1];
		for(int i=len-2;i>=0;i--){
			int max = 0;
			for(int j=i;j<len;j++){
				//考虑到数组越界的问题
				max = Math.max(nums[j] +((j+2)>=len ? 0 : memo[j + 2]), max);
				//System.out.println("max=" + max);
			}			
			memo[i] = max;
			//System.out.println("memo["+i+"]="+memo[i]);
		}
		return memo[0];
	}

算法思路3(动态规划):函数定义:考虑偷取 [0…x]范围的房子。

参考代码:

//leetcode 198. House Robber  动态规划  自下向上 考虑偷取 [0...x]范围的房子 (函数的定义)
	public static int rob3(int[] nums){
		int len = nums.length;
		if(len==0)
			return 0;
		int[] memo = new int[len];
		//[0...0]范围偷取的最大值为nums[0]
		memo[0] = nums[0];
		for(int i=1;i<len;i++){
			int max = 0;
			for(int j=i;j>=0;j--){
				max = Math.max(nums[j] +((j-2)<0 ? 0 : memo[j - 2]), max);
				//System.out.println("max=" + max);
			}			
			memo[i] = max;
			//System.out.println("memo["+i+"]="+memo[i]);
		}
		return memo[len-1];
	}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值