Leetcode 198 House Robber
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.
离散的问题求最大值,首先应该想到用DP。例如 nums = [2, 3, 1, 5, 7, 8]
dp[0] = 2, dp[1] = max(nums[0], nums[1]) 这两个点需要单独操作。 dp[i]的值取决于[i]这一家是不是要偷。如果不偷的话,dp[i] = dp[i-1]。如果偷的话,[i-1]这一家就不能偷,所以 dp[i] = dp[i-2] + nums[i]。所以dp[i] = max(dp[i-1], dp[i-2]+nums[i])。
def rob(self, nums):
"""
:type nums: List[int]
:rtype: int
"""
if not nums:
return 0
n = len(nums)
if n == 1:
return nums[0]
if n == 2:
return max(nums[0], nums[1]) dp = [0]*n
dp[0] = nums[0]
dp[1] = max(nums[1], nums[0]) for i in range(2, n):
dp[i] = max(dp[i-2]+nums[i], dp[i-1]) return dp[-1]
本质上并不需要保存dp list中的所有值,只需要保存两个,分别对应even and odd position。 这样的话可以写成:
def rob(self, nums):
"""
:type nums: List[int]
:rtype: int
"""
a, b = 0, 0
n = len(nums) for i in range(n):
if i % 2 == 0:
a += nums[i]
a = max(b, a)
else:
b += nums[i]
b = max(a, b) return max(a, b)
Leetcode 198 House Robber的更多相关文章
- 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] 198. House Robber 打家劫舍
You are a professional robber planning to rob houses along a street. Each house has a certain amount ...
- Java for LeetCode 198 House Robber
You are a professional robber planning to rob houses along a street. Each house has a certain amount ...
- (easy)LeetCode 198.House Robber
You are a professional robber planning to rob houses along a street. Each house has a certain amount ...
- Java [Leetcode 198]House Robber
题目描述: You are a professional robber planning to rob houses along a street. Each house has a certain ...
- [LeetCode] 198. House Robber _Easy tag: Dynamic Programming
You are a professional robber planning to rob houses along a street. Each house has a certain amount ...
- leetcode 198. House Robber (Easy)
https://leetcode.com/problems/house-robber/ 题意: 一维数组,相加不相邻的数组,返回最大的结果. 思路: 一开始思路就是DP,用一维数组保存dp[i]保存如 ...
- Leetcode 198 House Robber 动态规划
题意是强盗能隔个马抢马,看如何获得的价值最高 动态规划题需要考虑状态,阶段,还有状态转移,这个可以参考<动态规划经典教程>,网上有的下的,里面有大量的经典题目讲解 dp[i]表示到第i匹马 ...
- leetcode 198 House Robber I
function rob(nums) { if(!nums || nums.length === 0) { return 0; } else if(nums.length < 2){ retur ...
随机推荐
- redis-cache中的callback
这个是mybatis/redis-cache中关键类 RedisCache 的源码 /** * Copyright 2015 the original author or authors. * * ...
- Linux下命令行安装配置android sdk
首先, 你得有个VPN 参考以下三篇完成Android SDK的安装 https://www.digitalocean.com/community/tutorials/how-to-build-and ...
- Java手动添加SSL证书
出现错误为 SSLHandshakeException - unable to find valid certification path to requested target 在服务器上找到对应的 ...
- Flex String转Date
在Flex中日期字符串转为Date类型,可以使用静态方法DateFormatter.parseDateString(str:String):Date方法. 该方法支持的字符串格式包括: YYYY-MM ...
- R统计分析处理
[翻译]Awesome R资源大全中文版来了,全球最火的R工具包一网打尽,超过300+工具,还在等什么? 阅读目录 0.前言 1.集成开发环境 2.语法 3.数据操作 4.图形显示 5.HTML部件 ...
- Linux shell程序一
设计一个Shell程序,在/$HONE/test目录下建立50个目录,即user1-user50, 并设置每个目录的权限,其中其他用户的权限为:读:文件所有者的权限为: 读.写.执行:文件所有者所在组 ...
- 利用performance属性查看网页性能
一般我们可以通过浏览器的调试工具-网络面板,或者代理工具查看网页加载过程中的各个阶段的耗时.而利用window.performance属性则可以获得更为精确的原始数据,以毫秒为单位,精确到微秒. pe ...
- 继续研究NDK
继续研究NDK 我在阿里云服务器上搭建了Android ndk的开发平台,并且借助这一平台研究了NDK的内部细节. NDK提供了Android本地编程的接口,让你可以开发高效的依赖库,提高程序的速度, ...
- 【技术】JavaSE环境下JPA实体类自动注册
在没有容器支持的环境下,JPA的实体类(Entity)一般要在persistence.xml中逐个注册,类似下面这样: <?xml version="1.0" encodin ...
- 数据库MongoDB查询语句--持续更新
模糊查询: 包含字符串str : find({'name':/str/i}); {'name':/str/} 以str开头: {'name':/^str/} $in查询: 字段:{ field: ...