[抄题]:

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.

[暴力解法]:

时间分析:

空间分析:

[优化后]:

时间分析:

空间分析:

[奇葩输出条件]:

[奇葩corner case]:

[思维问题]:

不知道怎么处理首尾重复的问题:分情况讨论。从0-n-1, 1-n

[英文数据结构或算法,为什么不用别的数据结构或算法]:

[一句话思路]:

exclude必须是用上一次的结果i e,否则会越加越大。所以要把上一次的结果用i e保存起来。

[输入量]:空: 正常情况:特大:特小:程序里处理到的特殊情况:异常情况(不合法不合理的输入):

[画图]:

[一刷]:

[二刷]:

[三刷]:

[四刷]:

[五刷]:

[五分钟肉眼debug的结果]:

[总结]:

分情况讨论也是一种办法。

[复杂度]:Time complexity: O(n) Space complexity: O(1)

[算法思想:迭代/递归/分治/贪心]:

[关键模板化代码]:

[其他解法]:

[Follow Up]:

[LC给出的题目变变变]:

[代码风格] :

[是否头一次写此类driver funcion的代码] :

[潜台词] :

class Solution {
public int rob(int[] nums) {
//corner case
if (nums == null || nums.length == 0) return 0;
if (nums.length == 1) return nums[0]; //discuss in 2 ways
return Math.max(rob(nums, 0, nums.length - 2), rob(nums, 1, nums.length - 1));
} public int rob(int[] nums, int low, int high) {
//define include, exclude
int include = 0; int exclude = 0; //for loop, define i and e, and expand
for (int j = low; j <= high; j++) {
int i = include; int e = exclude;
include = e + nums[j];
exclude = Math.max(i, e);
} //return max
return Math.max(include, exclude);
}
}

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

  8. 213. House Robber II

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

  9. LeetCode 213. House Robber II

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

随机推荐

  1. Linux系统时钟的更改

    linux系统时钟有两个,一个是硬件时钟,即BIOS时间,就是我们进行CMOS设置时看到的时间,另一个是系统时钟,是linux系统Kernel时间. 查看.设置硬件时间: 查看系统硬件时钟 hwclo ...

  2. AndroidStudio 开发JNI

    Android版本不断更新,发现网上很多JNI的教程,都不太适用了,会遇到各种问题,今天自己来总结一个. NDK下载 我们下载NDK,有两种下载方式: 这是Google官方下载 点击下载NDK: 通过 ...

  3. putty安装和使用

    https://blog.csdn.net/l707941510/article/details/80520790

  4. calibre的注册表残留删除

    卸载calibre后,注册表仍有残留 for /f %a in ('reg query HKEY_CLASSES_ROOT /f calibre /k') do reg delete %a /f

  5. NPOI导出excel(2.0.6版本)

    public static void WriteExcel(System.Data.DataTable dt,string fileName) { NPOI.XSSF.UserModel.XSSFWo ...

  6. Makefile知识点总结

    1.=,:=,+=区别 = 是最基本的赋值 := 是覆盖之前的值 ?= 是如果没有被赋值过就赋予等号后面的值 += 是添加等号后面的值 .“=” make会将整个makefile展开后,再决定变量的值 ...

  7. 《Java程序设计》 第二周学习总结

    20175334 <Java程序设计>第二周学习总结 教材学习内容总结 了解Java编程风格 认识Java基本数据类型与数组 掌握Java运算符.表达式和语句 教材学习中的问题和解决过程 ...

  8. access数据库编号转换成统一3位数长度方法,不足3位前面补零

    select C_CUN+Format(Val(NZ(C_LB)),"000") from LBM 这条SQL只能在access数据库中执行,因为sql不支持NZ函数,而且c_lb ...

  9. React+ES6+Webpack深入浅出

    React已成为前端当下最热门的前端框架之一 , 其虚拟DOM和组件化开发让前端开发更富灵活性,而Webpack凭借它异步加载和可分离打包等优秀的特性,更为React的开发提供了便利.其优秀的特性不再 ...

  10. vue-cli脚手架build目录中的build.js配置文件

    该随笔收藏自: vue-cli脚手架build目录中的build.js配置文件 这个配置文件是命令npm run build 的入口配置文件,主要用于生产环境 由于这是一个系统的配置文件,将涉及很多的 ...