URL: https://leetcode.com/problems/house-robber-ii/

You are a professional robber planning to rob houses along a street. Each house has a certain amount of money stashed. All houses at this place are arranged in a circle. That means the first house is the neighbor of the last one. Meanwhile, 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.

Example 1:

Input: [2,3,2]
Output: 3
Explanation: You cannot rob house 1 (money = 2) and then rob house 3 (money = 2),
  because they are adjacent houses.

Example 2:

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.

解决方案:

Since it is a cyclied array, to make our dynamic programming alogrithm works, we need to break the cyclied array into two single-way arrays.

2-The priciple here is that either a particular house i-th can be robbed or not robbed at all.

For example 1 -> 3 -> 6 -> 5, we can break at the first position, so that the problem is divided to two parts,which are 1 -> 3 -> 6 (we choose to rob the first element, but not the last one.) and 3->6->5. Finally, we can reuse the House Robber I to solve these two sub-problems and pick out the optimal solution.

3-Here some of us may have the confusion that: we can rob house 1, it does not mean that we must rob house 1, whether we rob house 1 is depending on its next value. The correct complement of this statement is that: we should not(must not) rob house 1 ( and similarly, whether to rob the last house depends on the whole broken array.)

class Solution {
public int rob(int[] nums) {
if(nums == null || nums.length == 0) return 0; if(nums.length == 1) return nums[0]; int len = nums.length;
int[] amount = new int[len];
//The first case: we can rob house 1, definitely, we cannot rob the last one
amount[0] = nums[0];
// whether we rob house 1 is depending the relative value
amount[1] = Math.max(nums[0],nums[1]);
for(int idx =2; idx < len-1; idx ++){
amount[idx] = Math.max(amount[idx-1],amount[idx-2] + nums[idx]);
}
// the last element is zero. amount[len-1] by default is zero
int totalSumCaseOne = amount[len-2]; // The second case: we don't rob house 1 at all.
amount = new int[len];
amount[0] = 0;
amount[1] = nums[1];
for(int idx = 2 ;idx< len; idx++){
amount[idx] = Math.max(amount[idx-1],amount[idx-2] + nums[idx]);
}
int totalSumCaseTwo = amount[len-1]; //select the largest one
return Math.max(totalSumCaseOne,totalSumCaseTwo);
}
}

动态规划 - 213. House Robber II的更多相关文章

  1. 198. House Robber,213. House Robber II

    198. House Robber Total Accepted: 45873 Total Submissions: 142855 Difficulty: Easy You are a profess ...

  2. 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:二叉树下的不能相邻,求能 ...

  3. 【LeetCode】213. House Robber II

    House Robber II Note: This is an extension of House Robber. After robbing those houses on that stree ...

  4. 【刷题-LeetCode】213. House Robber II

    House Robber II You are a professional robber planning to rob houses along a street. Each house has ...

  5. 213. House Robber II(动态规划)

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

  6. [LeetCode] 213. House Robber II 打家劫舍之二

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

  7. [LeetCode] 213. House Robber II 打家劫舍 II

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

  8. 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 ...

  9. 213. House Robber II

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

随机推荐

  1. (转载) python3: beautifulsoup的使用

    转载: https://www.cnblogs.com/chimeiwangliang/p/8649003.htmlfrom bs4 import BeautifulSoup import reque ...

  2. day05-(validate&bootstred)

    网站分享: http://www.runoob.com/ 回顾: html:展示 文件 标签: <html> <head> <title></title> ...

  3. 如何修改Tomcat的默认项目发布路径

    tomcat默认的项目发布目录是/webapp/ROOT,如果想自定义发布目录,应该怎么办呢? 修改配置文件 首先,修改$tomcat/conf/server.xml文件. 在server.xml文件 ...

  4. nginx变量(日志log_format)

    nginx变量(日志log_format) HTTP请求变量 - arg_PARAMETER.http_HEADER.sent_http_HEADER 它是指http请求中的变量,举例: curl访问 ...

  5. mysql报错汇总

    一.启动mysql: Can't connect to local MySQL server through socket '/var/run/mysqld/mysqld.sock'  #/var/r ...

  6. 运维监控-Zabbix Server 使用微信 WeChat 告警

    运维监控-Zabbix Server 使用微信 WeChat 告警 作者:尹正杰 版权声明:原创作品,谢绝转载!否则将追究法律责任. 微信公众号告警每个一天只支持1000次告警,如果超出这个次数了就不 ...

  7. Docker笔记一:Docker介绍

    目录 什么是Docker? Docker的核心概念 Docker镜像命令 Docker容器命令 Docker实战 查看我的镜像 启动Redis Docker中国镜像加速 血与泪的教训 什么是Docke ...

  8. Linux记录-配置无密码登录

    1.互信的机器都执行 ssh-keygen -t rsa cat ~/.ssh/id_rsa.pub >> /home/hdfs/.ssh/authorized_keys chmod 60 ...

  9. hdu 2815 Mod Tree (exBSGS)

    http://acm.hdu.edu.cn/showproblem.php?pid=2815 //解 K^D ≡ N mod P #include<map> #include<cma ...

  10. c++进阶学习

    以后可能要做c++开发了..记录要看的书和可能用的技术,让自己有个学习的方向... 1. 语言基础 2. 算法与数据结构基础 3. 多线程开发基础  4. 数据库  5. 网络编程 6. 内存数据库技 ...