Leetcode 198. House Robber

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

原题链接:198. House Robber

  一句话理解题意,有个偷马贼晚上要偷尽可能值钱的马,但连续两头马被偷会触发报警,问他如何在不触发报警(不偷连续的两匹马)的情况下偷到总价值最高马,返回最高总价值。
  看到maximum,就应该想到这是应该求解最优的问题,一想到求解最优,一般除了暴力就是动态规划了。
  我们先来看看暴力解法,此题暴力还是比较复杂的,枚举每个位置的马偷或者不偷,用递归可以很简单的实现,然后再排除连续的情况。想想时间复杂度,已经到了惊人的O(2^n),一般n到20左右一般的计算机就抗不住了。
  如果是动态规划又会怎么样?如果robber在第i个位置,他如何保证在此位置获得最大的价值,他肯定有两种选择,偷或者不偷第i匹马。如果偷的情况下最大价值是啥?不偷又是啥?如果你想通了,你肯定会得到两种情况下的转态转移方程。偷第I批马 dp[1][i] = Math.max(dp[1][i-1], dp[0][i-1]+nums[i]),不偷 dp[0][i] = Math.max(dp[0][i-1], dp[1][i-1])。 这里我用 0和1表示不偷和偷。

代码如下:

public class Solution {
    public int rob(int[] nums) {
        if (0 == nums.length)
            return 0;
        int[][] dp = new int[2][nums.length];
        for (int i = 0; i < nums.length; i++) {
            if (0 == i) {
                dp[0][0] = 0;
                dp[1][0] = nums[0];
            }
            else if (1 == i) {
                dp[0][1] = nums[0];
                dp[1][1] = nums[1];
            }
            else {
                dp[0][i] = Math.max(dp[0][i-1], dp[1][i-1]);
                dp[1][i] = Math.max(dp[1][i-1], dp[0][i-1]+nums[i]);
            }
        }
        return Math.max(dp[0][nums.length-1], dp[1][nums.length-1]); 
    }
}
  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

xindoo

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值