LeetCode House Robber I and II

在这里,我将这两题放在一起来解决,因为这两道都是在Dynamic Programming(动态规划)的类型里面,所以放在一起比较好,只是第二题比第一题多了一个条件,其他都是一样的。

首先是第一题:

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.

题意:

一个小偷偷一条街道的居民,然后小偷不能偷直接相连的两个屋,那么如何能确保这个小偷偷的东西的价值最大。这道题可以采用非常典型的动态规划来做,考虑当前这个位置的值来自max(sums[i-1],nums[i-2] + sums[i-1])直到最后一项,然后计算出相应的和。

public class Solution 
{
    public int rob(int[] num) 
    {
        if(num==null || num.length==0)
            return 0;
        int n = num.length;
        int[] dp = new int[n+1];
        dp[0] = 0;
        dp[1] = num[0];
        for(int i = 2; i < n + 1; i++)
        {
           dp[i] = Math.max(dp[i-1], dp[i-2]+num[i-1]);       //其实这个公式的推导与之前的那个even和odd差不多
        }
        return dp[n];
    }
}
LeetCode House Robber II:

Note: This is an extension of House Robber.

After robbing those houses on that street, the thief has found himself a new place for his thievery so that he will not get too much attention. This time, all houses at this place are arranged in a circle. That means the first house is the neighbor of the last one. Meanwhile, the security system for these houses remain the same as for those in the previous street.

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.

题意:

此题是考虑将居民区生成环,也就是首尾相连,那么考虑两种情况的最大值,第一种情况是从第一个位置开始,然后到倒数第二个为止,因为要确保第一个元素和倒数最后一个元素不相连;第二种情况是从第二个位置开始,直到倒数第一个元素结束,然后计算相应的值。

public static int rob(int[] nums)
	{
		int length = nums.length;
		if(length == 0 || nums == null)
			return 0;
		if(length == 1)
			return nums[0];
		if(length == 2)
			return Integer.max(nums[0], nums[1]);
		int[] sum1 = new int[length];
		int[] sum2 = new int[length];
		sum1[0] = nums[0];
		for(int i = 1; i < length - 1; i++)
		{
			sum1[i] = Integer.max(sum1[i-1],(i == 1 ? 0 : sum1[i-2]) + nums[i]);
			//System.out.print(sum1[i] + "   ");
		}
		//System.out.println();
		int res = sum1[length - 2];
		sum2[1] = nums[1];
		for(int j = 2; j < length; j++)
		{
			sum2[j] = Integer.max(sum2[j-1],(j == 2 ? 0 : sum2[j-2]) + nums[j]);
			//System.out.print(sum2[j] + "  ");
		}
		int rex = sum2[length - 1];
		System.out.println();
		return Integer.max(res,rex);
	}




  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值