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 ...
随机推荐
- cannot find package "context"
导入 github.com/go-sql-driver/mysql 和 database/sql 时,报cannot find package "context"的错误因为go1 ...
- PythonStudy——数据类型转化 Data type conversion
类型转换 1.数字类型:int() | bool() | float() 2.str与int:int('10') | int('-10') | int('0') | float('-.5') | fl ...
- docker安装solr集群5.3.1
docker-compose.yml: version: '3' services: zookeeper-A: image: zookeeper:3.4.11 ports: - "12181 ...
- Innodb中MySQL如何快速删除2T的大表
转自:http://database.51cto.com/art/201808/582324.htm OK,这里就说了.假设,你有一个表erp,如果你直接进行下面的命令: drop table erp ...
- Java8-dateTimeFormatter
时间格式化LocalDate,DateTimeFormatter--->parse,ofParttern 伴随lambda表达式.streams以及一系列小优化,Java 8 推出了全新的日期时 ...
- js window.location用法
<script> //设置或获取 href 属性中跟在问号后面的部分. console.log(window.location.search)//设置或获取对象指定的文件名或路径conso ...
- problem:vue组件局部刷新,在组件销毁(destroyed)时取消刷新无效问题
场景: 一个群发消息列表(数组) 列表下有多条消息(元素) 每条正在发送的消息数据状态需要实时刷新,发送完成时需要显示成功提示符合且不需要刷新,然后3秒消失.首次显示列表时,已经成功的状态不显示这个成 ...
- Android Studio指定引用jnilibs 特定CPU架构的so库文件
稍微大一些的项目都会用到第三方库,所以不可避免的会有针对不同手机cpu架构的.so库文件 'x86', 'x86_64', 'mips', 'mips64' 'armeabi' ,'armeabi- ...
- linux添加zabbix service并开机自动启动
最近有个数据库相关操作后需要重启操作系统,重启后发现zabbix监控一直没有数据,迷了半天原来zabbix压根就没有启动.想了半天决定把zabbix添加到系统服务,并设置开机启动. 1.按一定的规则编 ...
- python中赋值,深拷贝,浅拷贝区别
这三种 的区别就是 复制的变量 是否是原变量的引用. 赋值:只是原变量的引用. 浅拷贝和深拷贝的区别 需要通过 子元素 区分 浅拷贝:子元素的 引用相同 深拷贝:所以引用都不相同,完全复制一份 这三种 ...