Leetcode: House Robber II
来源:互联网 发布:mac启动后黑屏 编辑:程序博客网 时间:2024/05/16 01:07
Question
Note: This is an extension of House Robber.
After robbing those houses on that street, the thief has found himself a new place for his thievery so that he will not get too much attention. This time, all houses at this place are arranged in a circle. That means the first house is the neighbor of the last one. Meanwhile, the security system for these houses remain the same as for those in the previous street.
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.
Credits:
Special thanks to @Freezen for adding this problem and creating all test cases.
Show Tags
Show Similar Problems
Solution
Analysis
Get idea from here.
Code
class Solution(object): def rob(self, nums): """ :type nums: List[int] :rtype: int """ if nums==[]: return 0 if len(nums)<=2: return max(nums) return max( self.helper(nums, 0, len(nums)-2), self.helper(nums, 1, len(nums)-1) ) def helper(self, nums, s, e): temp1 = 0 temp2 = nums[s] for ind in range(s+1, e+1): res = max( temp1 + nums[ind], temp2 ) temp1 = temp2 temp2 = res return res
Error Path
- 1.
for ind in range(s+1, e+1): res = max( temp1 + nums[ind], temp2 ) temp1 = temp2 temp2 = res
made a mistake on start and stop condition of loop
Take home message
For dynamic programming, we can save the storage complexity for some specific problem.
- LeetCode House Robber II
- [LeetCode] House Robber II
- [leetcode] House Robber II
- 【leetcode】House Robber II
- Leetcode: House Robber II
- leetcode House Robber II
- [leetcode] House Robber II
- [LeetCode]House Robber II
- leetcode--House Robber II
- leetcode House Robber II
- LeetCode House Robber II
- leetcode:House Robber II
- Leetcode: House Robber II
- House Robber II -- leetcode
- leetCode House Robber II
- [Leetcode]House Robber II
- Leetcode: House Robber II
- LeetCode House Robber II
- iOS Communicating with Authenticating HTTP Servers 与HTTP服务器通信认证 官方文档翻译(六)
- Android同一个xml布局文件在不同中显示效果不同
- Javascript鼠标滚轮编程
- IOS 学习 之 引用计数
- Android.mk 中打印调试信息
- Leetcode: House Robber II
- 利用Media Queries来实现IE hack
- 结巴分词学习笔记
- Tutorial 03: Playing Sound
- Java星打印代码
- layoutsubviews什么时候调用
- spring security组成
- sprinf函数使用详解
- c语言实现循环链表的基本操作