[抄题]:

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 caseif (nums == null || nums.length == 0) return 0;if (nums.length == 1) return nums[0];//discuss in 2 waysreturn 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, excludeint include = 0; int exclude = 0;//for loop, define i and e, and expandfor (int j = low; j <= high; j++) {int i = include; int e = exclude;include = e + nums[j];exclude = Math.max(i, e);}//return maxreturn Math.max(include, exclude);}
}

View Code

转载于:https://www.cnblogs.com/immiao0319/p/9515468.html

213. House Robber II 首尾相同的偷窃问题相关推荐

  1. 213. House Robber II

    一.题目 1.审题 2.分析 一个连续的数组,你能获取不连续的元素值代表的财富,数组首尾的两个元素算作连续,求能获得的最大财富值. 二.解答 1.思路: 在 198. House Robber 的基础 ...

  2. LeetCode 213 House Robber II Python

    题意:你是一个专业的小偷,计划偷窃沿街的房屋,每间房内都藏有一定的现金.这个地方所有的房屋都围成一圈,这意味着第一个房屋和最后一个房屋是紧挨着的.同时,相邻的房屋装有相互连通的防盗系统,如果两间相邻的 ...

  3. leetcode 213. House Robber II | 213. 打家劫舍 II(Java)

    题目 https://leetcode.com/problems/house-robber-ii/ 题解 这道题是「198. 打家劫舍」的进阶,和第 198 题的不同之处是,这道题中的房屋是首尾相连的 ...

  4. LeetCode 213. House Robber II(小偷游戏)

    原题网址:https://leetcode.com/problems/house-robber-ii/ Note: This is an extension of House Robber. Afte ...

  5. [leetcode] 213. House Robber II 解题报告

    题目链接:https://leetcode.com/problems/house-robber-ii/ Note: This is an extension of House Robber. Afte ...

  6. 【Leetcode】213. 打家劫舍II(House Robber II)

    Leetcode - 213 House Robber II (Medium) 题目描述:一个小偷沿着一条环形的街偷窃,给定数组表示每家屋子的金额,如果偷窃连续的两间屋子就会触发警报,求在不触发警报的 ...

  7. LeetCode 198, 213 House Robber

    198 House Robber DP 0~n-1     ans=dp[n-1] dp[i] = max(dp[i-2]+nums[i], dp[i-1])  i>=2 如果要输出偷了那些房子 ...

  8. Leetcode题目:House Robber II

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

  9. LeetCode OJ 之 House Robber II (抢劫犯 - 二)

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

最新文章

  1. SELECT INTO 和 INSERT INTO SELECT 两种表复制语句
  2. linux下安装jdk+tomcat+eclipse+mysql
  3. 同步通信和异步通信区别
  4. java.lang.VerifyError解决方案 Android
  5. day19 生成器函数
  6. Tomcat企业级应用
  7. 求职受性别限制?数据指明2021女性求职新方向
  8. FIFO,LRU,OPT的命中、调换过程
  9. kubernetes高级之创建只读文件系统以及只读asp.net core容器
  10. azm335x 串口配置
  11. jsp源码oracle数据库,JSP与oracle数据库交互案例
  12. Python开发者必知的 11 个 Python GUI 库,你用过几个?
  13. 【MyBatis笔记】08-输出类型
  14. HALCON 20.11:标定助手使用注意事项
  15. excel中,0不显示,负数显示红色
  16. 值得看的youtube频道
  17. 《中国电子报》访极通研发总监梁绍博
  18. Windows系统服务器系统日志在哪里查看?
  19. 警告:integer division in floating-point context
  20. 使用Arcgis画等值线图

热门文章

  1. 互联网协议入门-通俗易懂的讲计算机网络5层结构
  2. Bash shell 学习笔记六
  3. CSharp中的数组和ArrayList及List
  4. sam服务器是什么_使用SAM CLI将机器学习模型部署到无服务器后端
  5. Detection and Classification of Acoustic Scenes and Events(DCASE2013详细介绍)
  6. 利用task和电平敏感的always快设计经比较后重组信号的组合逻辑
  7. 奇异递归模板模式(Curiously Recurring Template Pattern,CRTP)
  8. java多线程意义_Java多线程学习之多线程的概念及意义
  9. mysql 分表索引_mysql中,分表查询和索引查询哪个更快?
  10. android程序root权限,android – 如何从源代码授予对特定应用程序的root访问权限而不是root权限?...