leetcode 198. House Robber

转载 2016年05月30日 17:55:50

原文链接:http://bluereader.org/article/125400253

题目

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.

public class Solution {
    //解题思路:
//对于第i个房间我们的选择是偷和不偷, 如果决定是偷 则第i-1个房间必须不偷 那么 这一步的就是 dp[i] = nums(i-1) + dpNotTake[i -1] , 假设dp[i]表示打劫到第i间房屋时累计取得的金钱最大值.
//如果是不偷, 那么上一步就无所谓是不是已经偷过, dp[i] = dp[i -1 ], 因此 dp[i] =max(dpNotTake[i-1 ] + nums(i-1), dp[i-1] );  其中dpNotTake[i-1]=dp[i-2]
//利用动态规划,状态转移方程:dp[i] = max(dp[i - 1], dp[i - 2] + num[i - 1])
//其中,dp[i]表示打劫到第i间房屋时累计取得的金钱最大值。
      public int rob(int[] nums) {
          int n = nums.length;
          if (n == 0) return 0;
          int[] dp = new int[n + 1];
          dp[0] = 0;
          dp[1] = nums[0];
          for (int i = 2; i < n + 1; ++i){
               dp[i] = Math.max(dp[i - 1], dp[i - 2] + nums[i - 1]);
          }
          return dp[n]; 
    }
}

相关文章推荐

LeetCode#198. House Robber

You are a professional robber planning to rob houses along a street. Each house has a certain amount...

Leetcode 198. House Robber

原题链接:198. House Robber   一句话理解题意,有个偷马贼晚上要偷尽可能值钱的马,但连续两头马被偷会触发报警,问他如何在不触发报警(不偷连续的两匹马)的情况下偷到总价值最高马,返...

leetcode 198 & 213:House Robber

一.House Robber I 的题意是:有一排商铺,强盗不能连续抢劫相邻的两家,否则会触动报警器,求强盗在不触动报警器的前提下,可以抢劫的最多金钱总和。(输入一维数组,从中选择若干个数,使得这些数...

LeetCode动态规划198. House Robber思路解析

LeetCode动态规划198. House Robber思路解析,用最容易理解的递归解释

leetcode 198 House Robber

今天看了一个华为西安研究院的一个女生代码大神的总结很有感悟,下面这句话送给大家:只有好的程序员才能写出人类可以理解的代码You are a professional robber planning t...

Leetcode 198. House Robber

You are a professional robber planning to rob houses along a street. Each house has a certain amount...

Leetcode[198]-House Robber

You are a professional robber planning to rob houses along a street. Each house has a certain amount...

Leetcode_198_House Robber

本文是在学习中的总结,欢迎转载但请注明出处:http://blog.csdn.net/pistolove/article/details/47680663 You are a profes...

LeetCode - 198. House Robber

每一个house都有rob和not rob两种状态,所以我们可以使用一个[n][2]的矩阵来记录每个房子被抢和不被抢的情况下所累计的钱的数量,这道动态规划问题的四要素如下: state: f[i][...

leetcode 198. House Robber

you are a professional robber planning to rob houses along a street. Each house has a certain amount...
内容举报
返回顶部
收藏助手
不良信息举报
您举报文章:深度学习:神经网络中的前向传播和反向传播算法推导
举报原因:
原因补充:

(最多只允许输入30个字)