Leetcode 198 House Robber 动态规划
题意是强盗能隔个马抢马,看如何获得的价值最高
动态规划题需要考虑状态,阶段,还有状态转移,这个可以参考《动态规划经典教程》,网上有的下的,里面有大量的经典题目讲解
dp[i]表示到第i匹马时的最大价值是多少,
因此所有的dp[i] = max(dp[i-2]+nums[i],dp[i-1]) (其中dp[0] = nums[0] dp[1] = max(nums[0],nums[1]);
class Solution {
public:
int rob(vector<int>& nums) {
vector<int> dp(nums.size(),);
if (nums.size() < )
{
return ;
}
if (nums.size() < )
{
return nums[];
}
dp[] = nums[];
dp[] = max(nums[],nums[]);
for (int i = ;i<dp.size();++i)
{
dp[i] = max(dp[i -] + nums[i], dp[i-]);
}
int ans = ;
for (int i = ;i<dp.size();++i)
{
ans = max(ans, dp[i]);
}
return ans;
}
};
Leetcode 198 House Robber 动态规划的更多相关文章
- [LeetCode] 198. 打家劫舍II ☆☆☆(动态规划)
描述 你是一个专业的小偷,计划偷窃沿街的房屋,每间房内都藏有一定的现金.这个地方所有的房屋都围成一圈,这意味着第一个房屋和最后一个房屋是紧挨着的.同时,相邻的房屋装有相互连通的防盗系统,如果两间相邻的 ...
- 198. House Robber(动态规划)
198. House Robber You are a professional robber planning to rob houses along a street. Each house ha ...
- 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 ...
- (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
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 ...
- [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 ...
随机推荐
- [刘阳Java]_Java技术有哪些学习重点_第1讲
纵观网上有很多非常好的对Java技术的总结,这里我个人谈谈对Java技术学习有哪些重点内容[浮浅之见] Java环境搭建 Java基础语法 Java面向对象编程 Java异常 Java IO流 Jav ...
- AVL树模板
///AVL树模板 typedef struct Node ///树的节点 { int val,data; int h; ///以当前结点为根结点的数的高度 int bf; ///平衡因子(左子树高度 ...
- iOS中两个APP之间的跳转和通信
app间的跳转 一:在第一个app首先要做下面这些操作: 1.在info.plist文件中的Information Property List下添加一项:URL types. 2.点开URL type ...
- mormot 数据集转换为JSON字串
mormot 数据集转换为JSON字串 unit Unit1; interface uses Windows, Messages, SysUtils, Variants, Classes, Graph ...
- deepdetect 用c++11写的机器学习caffe和XGBoost API 接口
https://github.com/beniz/deepdetect DeepDetect (http://www.deepdetect.com/) is a machine learning AP ...
- AT91-PWM应用
步骤1: make menuconfig配置内核, 开启PWM输出功能. Device Drivers ---> Misc devices ---> <*>Atmel AT3 ...
- 20145225 《Java程序设计》 第3周学习总结
20145225<Java程序设计> 第3周学习总结 教材学习内容总结 4.1类与对象 相当于设计图纸,用"new"创建的对象,就是依据设计图做成的成品 . 例(定义C ...
- HTTP请求响应对照表
1)请求(客户端->服务端[request]) GET(请求的方式) /books/java.html(请求的目标资源) HTTP/1.1(请求采用的协议和版本号) Accept: */*(客户 ...
- 使用VPN服务器解决公司不能上淘宝的问题
很多公司为了保证员工的效率,通常采用屏蔽端口的方法屏蔽掉了一些网站,比如淘宝.QQ网页版等,这样做虽然也是公司的迫不得已,但是也有点不人性化,毕竟非上班时间也是上不去此类网站的.前些日子电商大站,抢不 ...
- [更新中]并发和并行(Concurrency and Parallelism)
书籍的简称: CSPPSE: Computer System: a programmer's perspective Second Edition 术语并发是一个通用的概念, 指同时具有多个活动的系统 ...