[抄题]:

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. 剑指offer 1.数组 二维数组中查找

    题目描述 在一个二维数组中(每个一维数组的长度相同), 每一行都按照从左到右递增的顺序排序, 每一列都按照从上到下递增的顺序排序. 请完成一个函数, 输入这样的一个二维数组和一个整数,判断数组中是否含 ...

  2. localStorage小结

    使用HTML5可以在本地存储用户的浏览数据.. 什么是 HTML5 Web 存储? 在HTML5中,新加入了一个localStorage特性,这个特性主要是用来作为本地存储来使用的,解决了cookie ...

  3. [蓝桥杯]ALGO-20.算法训练_求先序排列

    问题描述 给出一棵二叉树的中序与后序排列.求出它的先序排列.(约定树结点用不同的大写字母表示,长度<=). 输入格式 两行,每行一个字符串,分别表示中序和后序排列 输出格式 一个字符串,表示所求 ...

  4. Centos7.5 安装高版本Cmake 3.6.2

    下载Cmake wget https://cmake.org/files/v3.6/cmake-3.6.2.tar.gz 解压Cmake tar xvf cmake-3.6.2.tar.gz & ...

  5. SAS PROC PRINT 常用选项和语句说明

    常用选项1.使用选项OBS=修改观测序号标签2.使用NOOBS选项不显示观测序号列3.使用ID语句在输出中取代观测序号列4.使用VAR选择输出的变量5.使用WHERE语句选择输出的观测6.使用数据集选 ...

  6. Python3网络爬虫(四):使用User Agent和代理IP隐藏身份《转》

    https://blog.csdn.net/c406495762/article/details/60137956 运行平台:Windows Python版本:Python3.x IDE:Sublim ...

  7. BZOJ 4872 luogu P3750 [六省联考2017]分手是祝愿

    4872: [Shoi2017]分手是祝愿 Time Limit: 20 Sec  Memory Limit: 512 MB[Submit][Status][Discuss] Description ...

  8. 203. 阿里jetcache

    [视频&交流平台] àSpringBoot视频:http://t.cn/R3QepWG à SpringCloud视频:http://t.cn/R3QeRZc à Spring Boot源码: ...

  9. jquery.cookie用法及其注意点

    jquery.cookie是一个轻量级的cookie插件,由于已被封装好,可拿来即用. 基本的创建.读取.删除见另一篇文章 浅谈localStorage.sessionStorage 与cookie  ...

  10. 多进程模块 multiprocessing

    由于GIL的存在,python中的多线程其实并不是真正的多线程,如果想要充分地使用多核CPU的资源,在python中大部分情况需要使用多进程. multiprocessing包是Python中的多进程 ...