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. 类型擦除对Java调用Kotlin的影响

    @JvmName: 扩展方法相关: 先来定义一个扩展方法: 好,接下来再来定义一个扩展函数: 此时报错了..看一下错误提示: 其中给的提示有点奇怪,第一个是很明显咱们的扩展函数木有接收参数嘛,为啥提示 ...

  2. 【POJ3714】Raid:平面最近点对

    Description After successive failures in the battles against the Union, the Empire retreated to its ...

  3. 基于qemu和unicorn的Fuzz技术分析

    前言 本文主要介绍如果使用 qemu 和 unicorn 来搜集程序执行的覆盖率信息以及如何把搜集到的覆盖率信息反馈到 fuzzer 中辅助 fuzz 的进行. AFL Fork Server 为了后 ...

  4. python打造seo必备工具-自动查询排名

    因为工作需要,利用业余时间开发的,可以查询百度排名+360排名工具,附上代码. #360搜索排名查询 # -*- coding=utf-8 -*- import requests from lxml ...

  5. Python 代码混淆和加密技术

    动机 Python进行商业开发时, 需要有一定的安全意识, 为了不被轻易的逆向. 混淆和加密就有所必要了. 混淆 为了增加代码阅读的难度, 源代码的混淆非常必要, 一个在线的Python代码混淆网站. ...

  6. LeetCode 919. Complete Binary Tree Inserter

    原题链接在这里:https://leetcode.com/problems/complete-binary-tree-inserter/ 题目: A complete binary tree is a ...

  7. JavaScript基础07——BOM

    BOM概念   BOM是Browser Object Model的缩写,简称浏览器对象模型.这个对象就是window         BOM提供了独立于内容而与浏览器窗口进行交互的对象         ...

  8. 异步编程(回调函数,promise)

    一.回调函数 ①概念:一般情况下,程序会时常通过API调用库里所预先备好的函数.但是有些库函数却要求应用先传给它一个函数,好在合适的时候调用,以完成目标任务.这个被传入的.后又被调用的函数就称为回调函 ...

  9. vue中前端弹窗队列展示

    在前端写一个弹窗可能很简单,那如果同时有多个弹窗呢 这样的话就要考虑弹窗的展示问题,肯定是不能叠加在一起的,这时候就要通过队列(先进先出)来展示 思路就是根据队列来实现,至于具体的实现方式,可以在项目 ...

  10. 【loj2985】【WC2019】I君的商店

    题目 交互题: 有\(n\)个物品,每个物品的价格为0或者1; 给出为1的物品的个数奇偶性k,并保证至少有一个价格为1: 每次可以询问一个集合S的另一个集合T的价值和的大小,交互库会返回>=或者 ...