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 ...
随机推荐
- ESXi 6.0 配置
ESXi 6.0 添加静态路由 首先打开ESXi的SSH服务, 在Configuration -> Security Profile -> Services , start SSH 用管理 ...
- 洛谷 U4792 Acheing 单调队列
U4792 Acheing 5通过 43提交 题目提供者Acheing 标签 难度尚无评定 提交 最新讨论 暂时没有讨论 题目背景 题目并没有什么含义,只是想宣传一下自己的博客,Acheing.com ...
- DEDECMS之二 如何修改模板页
使用织梦系统最经常是为了仿站,那么模板应该怎么改? 这里主要谈谈关于比较常用的几个模板页 网站主页.列表页.内容页.栏目的调用 1.主页模板 常用组合方法:index.htm + head.htm + ...
- Activity中获取view的高度和宽度为0的原因以及解决方案
在activity中可以调用View.getWidth.View.getHeight().View.getMeasuredWidth() .View.getgetMeasuredHeight()来获得 ...
- Theano2.1.12-基础知识之使用GPU
来自:http://deeplearning.net/software/theano/tutorial/using_gpu.html using the GPU 想要看GPU的介绍性的讨论和对密集并行 ...
- 前端科普文—为什么<!DOCTYPE> 不可或缺
When question comes 你一定在 HTML 页面最前面看到过这样一行代码(比如 百度): <!DOCTYPE html> 或者说类似这样的(比如 博客园-韩子迟 PS:博客 ...
- JSON拾遗
最近开始翻<JavaScript高级程序设计>,其实很多大师级人物都推荐这本书为JavaScript入门级读物.因为第20章 JSON篇幅最小,而且以前也写过一篇JSON的总结JSON简介 ...
- 又发现一个msdn的坑
一个类型里面有两个属性仅仅是大小写区别,可是IIS不区分大小写,问:如何才能查看两个属性里面的文档那? http://msdn.microsoft.com/en-us/library/microsof ...
- C语言之Sleep函数
Sleep函数: 功 能: 执行挂起一段时间 用 法: unsigned sleep(unsigned seconds); 注意: 在VC中使用带上头文件#include <windows.h& ...
- AlertDialog之常见对话框(单选对话框、多选对话框、进度条对话框)
单选对话框,顾名思义就是只能选一项(setSingleChoiceItems(Items,)) public void click(View v){ //创建对话框类 AlertDialog.Buil ...