213. House Robber II 首尾相同的偷窃问题
[抄题]:
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 首尾相同的偷窃问题的更多相关文章
- 198. House Robber,213. House Robber II
198. House Robber Total Accepted: 45873 Total Submissions: 142855 Difficulty: Easy You are a profess ...
- 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:二叉树下的不能相邻,求能 ...
- 【LeetCode】213. House Robber II
House Robber II Note: This is an extension of House Robber. After robbing those houses on that stree ...
- 【刷题-LeetCode】213. House Robber II
House Robber II You are a professional robber planning to rob houses along a street. Each house has ...
- 213. House Robber II(动态规划)
You are a professional robber planning to rob houses along a street. Each house has a certain amount ...
- [LeetCode] 213. House Robber II 打家劫舍之二
You are a professional robber planning to rob houses along a street. Each house has a certain amount ...
- 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 ...
- 213. House Robber II
题目: Note: This is an extension of House Robber. After robbing those houses on that street, the thief ...
- LeetCode 213. House Robber II
Note: This is an extension of House Robber. After robbing those houses on that street, the thief has ...
随机推荐
- poj2228 Naptime【(环结构)线性DP】
Naptime Time Limit: 1000MS Memory Limit: 65536K Total Submissions:3374 Accepted: 1281 Descriptio ...
- PythonStudy1——Python 值拷贝 浅拷贝 深拷贝
拷贝:对值进行复制的过程 # 值拷贝:应用场景最多 ls = [1, 'abc', [10]] ls1 = ls # ls1直接将ls中存放的地址拿过来 # ls内部的值发生任何变化,ls1都会随 ...
- C# ZipHelper C#公共类 -- ZipArchive实现压缩和解压
从网上找来个ZipArchive来压缩和解压缩的类,供参考吧 /******************************************************************** ...
- mac 中host设置方法
在开发中,有的接口为了安全考虑,只能通过指定的域名去反问,这时本地启动的 localhost 就无法获取到数据,需要去更改电脑的host文件配置,下面介绍mac 电脑的设置方法 1. 打开终端,输入一 ...
- Linux内核分析第二次作业
这周学习了<庖丁解牛Linux内核分析>并且学习了实验楼的相关知识. 在实验楼的虚拟环境下编写代码: 通过gcc编译后,使用查看文件命令:cat -n 20189223.c 在vim中, ...
- [转]vs2010用 boost.python 编译c++类库 供python调用
转自:http://blog.csdn.net/wyljz/article/details/6307952 VS2010建立一个空的DLL 项目属性中配置如下 链接器里的附加库目录加入,python/ ...
- DNS基础
什么是DNS? DNS--Domain name system,域名系统,简单来说就是域名和IP地址间的映射关系.当你在浏览器的地址栏输入网址(或域名,如 www,baidu.com)的时候,在网络中 ...
- Bootstrap Tooltip 显示换行
<a class="pink" href="#" data-toggle="tooltip" data-placement=" ...
- django模板语言循环字典,及forloop
views: from django.shortcuts import render,redirect from django.shortcuts import HttpResponse # Crea ...
- linux中telnet后退出连接窗口的方法?
linux中telnet后退出连接窗口 [root@a cron]# telnet www.baidu.com 80Trying 115.239.211.112...Connected to www. ...