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

解题思路:

dp问题 dp[i]=nums[i]+Math.max(dp[i-2], dp[i-3])

JAVA实现如下:

    public int rob(int[] nums) {
if(nums==null||nums.length==0)
return 0;
if(nums.length<=2)
return Math.max(nums[0], nums[nums.length-1]);
int[] dp=new int[nums.length];
dp[0]=nums[0];
dp[1]=nums[1];
dp[2]=nums[0]+nums[2];
for(int i=3;i<nums.length;i++)
dp[i]=nums[i]+Math.max(dp[i-2], dp[i-3]);
return Math.max(dp[nums.length-1], dp[nums.length-2]);
}

Java for LeetCode 198 House Robber的更多相关文章

  1. leetcode 198. House Robber 、 213. House Robber II 、337. House Robber III 、256. Paint House(lintcode 515) 、265. Paint House II(lintcode 516) 、276. Paint Fence(lintcode 514)

    House Robber:不能相邻,求能获得的最大值 House Robber II:不能相邻且第一个和最后一个不能同时取,求能获得的最大值 House Robber III:二叉树下的不能相邻,求能 ...

  2. Java [Leetcode 198]House Robber

    题目描述: You are a professional robber planning to rob houses along a street. Each house has a certain ...

  3. [LeetCode] 198. House Robber 打家劫舍

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

  4. Java for LeetCode 213 House Robber II

    Note: This is an extension of House Robber. After robbing those houses on that street, the thief has ...

  5. Leetcode 198 House Robber

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

  6. (easy)LeetCode 198.House Robber

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

  7. [LeetCode] 198. House Robber _Easy tag: Dynamic Programming

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

  8. leetcode 198. House Robber (Easy)

    https://leetcode.com/problems/house-robber/ 题意: 一维数组,相加不相邻的数组,返回最大的结果. 思路: 一开始思路就是DP,用一维数组保存dp[i]保存如 ...

  9. Java实现 LeetCode 198 打家劫舍

    198. 打家劫舍 你是一个专业的小偷,计划偷窃沿街的房屋.每间房内都藏有一定的现金,影响你偷窃的唯一制约因素就是相邻的房屋装有相互连通的防盗系统,如果两间相邻的房屋在同一晚上被小偷闯入,系统会自动报 ...

随机推荐

  1. yield实例

    如下 # __author__ = liukun # coding:utf-8 def it(): print ('hello') yield 1 yield 1 a= it() print(&quo ...

  2. linux中为什么已经是root用户仍不能执行程序

    .sh文件 ,获取root权限,提示Permission Denied. 这是因为文件本身没有可执行特性. chmod +x a.sh chmod 755 a.sh

  3. POJ3714 Raid

    Raid Time Limit: 5000MS   Memory Limit: 65536K Total Submissions: 10625   Accepted: 3192 Description ...

  4. Type-Length-Value编码

    Within data communication protocols, optional information may be encoded as a type-length-value or T ...

  5. event driven的一些概念

    1. event :Something that happens during your application that requires a response. 2.event object:Th ...

  6. Model1模式的学生信息增删改查

    Student.java package entity; public class Student { private int stuid; private String stuname; priva ...

  7. 最小圆覆盖(Smallest Enclosing Discs)

    随机增量算法(a randomized incremental algorithm) #define sqr(x) ((x)*(x)) #define EPS 1e-4 struct P{ doubl ...

  8. UVA116Unidirectional TSP(DP+逆推)

    http://acm.hust.edu.cn/vjudge/problem/viewProblem.action?id=18206 题意:M*N的数阵,从左边一列到右边一列走过的数的和的最小.并输出路 ...

  9. poj1733Parity game

    Parity game Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 7288   Accepted: 2833 Descr ...

  10. hud 2502 月之数

    I think: AC : import java.util.Scanner; public class Main { public static void main(String[] args) { ...