198. House Robber (dp)
2016-05-11 15:29
435 查看
198. House Robber
My SubmissionsQuestion
Editorial Solution
Total Accepted: 68856 Total
Submissions: 199648 Difficulty: Easy
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.
Subscribe to see which companies asked this question
Show Tags
Show Similar Problems
Have you met this question in a real interview?
Yes
No
Discuss
class Solution {
public:
int rob(vector<int>& nums) {
memset(dp,0,sizeof(dp));
int len=nums.size();
for(int i=0;i<len;i++)
{
dp[i][0]=0;
dp[i][1]=nums[i];
if(i>=1)
{
dp[i][0]+=max(dp[i-1][0],dp[i-1][1]);
dp[i][1]+=dp[i-1][0];
}
}
int ans=0;
for(int i=0;i<len;i++)
ans=max(max(dp[i][0],dp[i][1]),ans);
return ans;
}
private:
int dp[1005][2];
};
相关文章推荐
- Ionic 让你一见钟情的移动App开发框架
- 【C语言】截取字符串实现
- ISE操作问题点集合
- 各种js方法
- 第十二周 运算符重载阅读程序
- eclipse 导入工程出现问题:JRE System Libreary[jre1.6.0_05](unbound)
- 判断文件是否写入完毕
- Android关于buildToolVersion与CompileSdkVersion的区别
- 学习日记_SSH框架web.xml配置文件篇
- linux下php环境配置
- 替代数据库字段中的某个字符串
- STM32F407时钟树
- 关于socket通信的理解,socket连接通过IP+端口号的方式,那么如果两个服务器端的应用在同一台机器上,并且位于同一个tomcat容器下,那么岂不是两个服务器都收到了客户端发送的信息
- Netty 示例代码注解
- ubuntu 16.04 安装PhpMyAdmin
- 机器学习—逻辑回归理论简介
- java.lang.NoClassDefFoundError 异常解读
- java.lang.NoClassDefFoundError 异常解读
- Linux文件系统基本操作(mount挂载,umount卸载)
- Android自定义属性