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 题目汇总

[LeetCode] 213. House Robber II 打家劫舍 II的更多相关文章

  1. [LeetCode] 337. House Robber III 打家劫舍 III

    The thief has found himself a new place for his thievery again. There is only one entrance to this a ...

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

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

  3. 213 House Robber II 打家劫舍 II

    注意事项: 这是 打家劫舍 的延伸.在上次盗窃完一条街道之后,窃贼又转到了一个新的地方,这样他就不会引起太多注意.这一次,这个地方的所有房屋都围成一圈.这意味着第一个房子是最后一个是紧挨着的.同时,这 ...

  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 213. House Robber II

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

  6. [leetcode] #213 House Robber II Medium (medium)

    原题链接 比子母题House Robber多了一个条件:偷了0以后,第n-1间房子不能偷. 转换思路为求偷盗[0,n-1)之间,以及[1,n)之间的最大值. 用两个DP,分别保存偷不偷第0间房的情况. ...

  7. [LeetCode] 337. House Robber III 打家劫舍之三

    The thief has found himself a new place for his thievery again. There is only one entrance to this a ...

  8. LeetCode 198. 打家劫舍(House Robber)LeetCode 213. 打家劫舍 II(House Robber II)

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

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

随机推荐

  1. UVALive - 4097:Yungom(逼近 贪心)(DP)

    pro:有D个字母,每个字母有自己的权值,现状需要用它们拼出N个单词,使得这些单词互相不为另外一个的前缀. 且单词的权值和最小.D<=200; N<=200; sol:如果建立字典树,那个 ...

  2. Linux——配置maven

    前言 Maven是一个项目管理工具,它包含了一个项目对象模型 (Project Object Model),一组标准集合,一个项目生命周期(Project Lifecycle),一个依赖管理系统(De ...

  3. java构建简单的HTTP服务器

    使用Java技术构建Web应用时, 我们通常离不开tomcat和jetty之类的servlet容器,这些Web服务器功能强大,性能强劲,深受欢迎,是运行大型Web应用的必备神器. 虽然Java的设计初 ...

  4. 20199302《Linux内核原理与分析》第十二周作业

    ShellShock攻击实验 什么是ShellShock? Shellshock,又称Bashdoor,是在Unix中广泛使用的Bash shell中的一个安全漏洞,首次于2014年9月24日公开.许 ...

  5. .net core 根据已有数据库创建实体Model

    这三个引用需要与.net core 版本一致,否则后续其他操作时会出错 可以到NuGET包中找到对应的版本然后添加,或者使用一下语句将版本号修改为.net core对应的版本然后执行 Install- ...

  6. CollectionUtils.select用法

    import java.util.ArrayList;import java.util.List; import org.apache.commons.collections.CollectionUt ...

  7. 内核用户模式调试支持(Dbgk)

    简介 将详细分析Windows调试的内核模式接口.希望读者对C和通用NT内核体系结构和语义有一些基本的了解.此外,这并不是介绍什么是调试或如何编写调试器.它可以作为经验丰富的调试器编写人员或好奇的安全 ...

  8. Redis存储Hash

    结构: 1.存取操作: (1)单个存取: (2)一次存储多个,单个取出: 一次取出多个·: (3)取出全部的键和值: 2.删除: (1)删除单个键值对: (2)删除所有键值对: 3.增加数值: 4.减 ...

  9. 使用vault pki engine 方便的管理证书

    vault 是一个很方便的secret .敏感数据管理工具,当前的版本已经包含了UI,使用起来很方便 以下演示一个简单的pki 管理 项目使用docker-compose 运行,为了简单使用单机开发模 ...

  10. 开源项目 02 HttpLib

    using JumpKick.HttpLib; using Newtonsoft.Json; using System; using System.Collections.Generic; using ...