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.

Credits:
Special thanks to @Freezen for adding this problem and creating all test cases.

198. House Robber 的拓展,现在房子排成了一个圆圈,抢第一个屋子就不能抢最后一个屋子,抢最后一个屋子就不能抢第一个屋子。

解法:还是动态规划DP,分别算出这两个条件下的最大抢劫金额,然后取更大的就行。

Java:

public class Solution {

    public int rob(int[] nums) {
return Math.max(rob(nums, 0), rob(nums, 1));
} public int rob(int[] nums, int offset) {
// 如果长度过小,则直接返回结果
if(nums.length <= 1 + offset){
return nums.length <= offset ? 0 : nums[0 + offset];
}
int a = nums[0 + offset];
// 如果offset是1,则从下标为1的元素开始计算,所以要比较nums[1]和nums[2]
int b = Math.max(nums[0 + offset], nums[1 + offset]);
// 对于不抢劫最后一个房子的情况,i要小于nums.length - 1
for(int i = 2 + offset; i < nums.length - 1 + offset; i++){
int tmp = b;
b = Math.max(a + nums[i], b);
a = tmp;
}
return b;
}
}

Python:

class Solution:
# @param {integer[]} nums
# @return {integer}
def rob(self, nums):
if len(nums) == 0:
return 0 if len(nums) == 1:
return nums[0] return max(self.robRange(nums, 0, len(nums) - 1),\
self.robRange(nums, 1, len(nums))) def robRange(self, nums, start, end):
num_i, num_i_1 = nums[start], 0
for i in xrange(start + 1, end):
num_i_1, num_i_2 = num_i, num_i_1
num_i = max(nums[i] + num_i_2, num_i_1); return num_i

C++:  

class Solution {
public:
int rob(vector<int>& nums) {
if (nums.size() == 0) {
return 0;
}
if (nums.size() == 1) {
return nums[0];
} return max(robRange(nums, 0, nums.size() - 1), // Include the first one of nums without the last one.
robRange(nums, 1, nums.size())); // Include the last one of nums without the first one.
} int robRange(vector<int>& nums, int start, int end) {
int num_i = nums[start], num_i_1 = 0, num_i_2 = 0;
for (int i = start + 1; i < end; ++i) {
num_i_2 = num_i_1;
num_i_1 = num_i;
num_i = max(nums[i] + num_i_2, num_i_1);
}
return num_i;
}
};

C++:

class Solution {
public:
int rob(vector<int>& nums) {
if (nums.size() <= 1) return nums.empty() ? 0 : nums[0];
return max(rob(nums, 0, nums.size() - 1), rob(nums, 1, nums.size()));
}
int rob(vector<int> &nums, int left, int right) {
int a = 0, b = 0;
for (int i = left; i < right; ++i) {
int m = a, n = b;
a = n + nums[i];
b = max(m, n);
}
return max(a, b);
}
};

类似题目:

[LeetCode] 198. House Robber 打家劫舍

  

All LeetCode Questions List 题目汇总

最新文章

  1. sphinx 配置文件全解析
  2. 【C#】Json数据 排版算法
  3. action script 3如何检测播放器域
  4. 5.21_启程日本二面_1 vs 1
  5. LLVM language 参考手册(译)(6)
  6. POJ 1330 Nearest Common Ancestors(求最近的公共祖先)
  7. Head First 设计模式系列之一----模板模式(java版)
  8. Android从网络中获取xml文件并解析数据
  9. Thinkphp 3.0版本上传文件加图片缩略图实例解析
  10. java--局部类只能访问外包方法的final局部成员
  11. 百度地图隐藏缩放控件比例尺Logo
  12. jQuery鼠标移入移出(冒泡版和无冒泡版)
  13. 2018-2019-2 20165239 《网络对抗技术》Kali的安装 第一周
  14. HDU 6495 冰水挑战
  15. 剑指offer 二叉搜索树和双向链表
  16. 简单的线程Thread使用
  17. 我的第一个 react redux demo
  18. SNF框架及机器人2018年1-9月份升级内容
  19. SIFT算法详解
  20. Android 查看system/bin目录下支持哪些命令?

热门文章

  1. django 项目需要注意的一些点
  2. mybatis从入门到精通
  3. Python 代码混淆和加密技术
  4. SpringMVC_原理(转)
  5. IGC(Interleaved Group Convolutions)
  6. SQL Server 父子迭代查询语句,树状查询
  7. 14.go内置的rate包学习2(有花操作,必看)
  8. 过滤器的API
  9. zabbix server内存突然飙升
  10. 计蒜客 39268.Tasks-签到 (The 2019 ACM-ICPC China Shannxi Provincial Programming Contest A.) 2019ICPC西安邀请赛现场赛重现赛