Leetcode: House Robber
来源:互联网 发布:openal是什么软件 编辑:程序博客网 时间:2024/06/14 18:22
Question
You are a professional robber planning to rob houses along a street. Each house has a certain amount of money stashed, the only constraint stopping you from robbing each of them is that 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.
Credits:
Special thanks to @ifanchu for adding this problem and creating all test cases. Also thanks to @ts for adding additional test cases.
Hide Tags Dynamic Programming
Analysis
Solution
v1. Space Complexity: O(n)
class Solution: # @param {integer[]} nums # @return {integer} def rob(self, nums): if len(nums)==0: return 0 if len(nums)<=2: return max(nums) res = [0]*len(nums) res[0],res[1] = nums[0],max(nums[0:2]) for i in range(2,len(res)): res[i] = max(res[i-1],res[i-2]+nums[i]) return res[-1]
v2. Space Complexity O(1)
class Solution: # @param {integer[]} nums # @return {integer} def rob(self, nums): if len(nums)==0: return 0 if len(nums)<=2: return max(nums) l1,l2 = nums[0],max(nums[0:2]) res = 0 for i in range(2,len(nums)): temp = l2 l2 = max(l2, l1+nums[i]) l1 = temp return l2
0 0
- 【dp】Leetcode House Robber&& House Robber II
- 【leetcode】House Robber && House Robber II
- LeetCode --- House Robber & House Robber II
- LeetCode OJ House Robber
- Leetcode: House Robber
- Leetcode 198: House Robber
- House Robber - LeetCode
- LeetCode 之 House Robber
- leetcode--House Robber
- [leetcode]47 House Robber
- [LeetCode] House Robber
- LeetCode :House Robber
- [leetcode] House Robber
- [leetcode]House Robber
- [LeetCode]198.House Robber
- LeetCode House Robber题解
- leetcode:House Robber
- [LeetCode]House Robber
- 深入理解TAILQ队列
- 寻找相同元素的指针
- android toast设置比Toast.LENGTH_SHORT还短的持续时间
- AppCan “移动互联网+” 重构传统企业移动化转型
- SpringMVC访问静态资源的三种方式
- Leetcode: House Robber
- FILETIME, SYSTEMTIME 与 time_t 相互转换
- nagios的安装配置
- Linux下动态加载
- PE10.4工程导入MDK5.11a编译环境
- 多点触控
- 为Ubuntu添加一块新硬盘步骤
- mysql 乱码 解决方案
- 送上最新鲜的互联网行业新闻-【2015-05-14】