【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.
Solution:动态规划,当前rob到第n幢房子获得的最大收益为maxV(n)=max(maxV(n-2)+nums[n],maxV(n-1))
class Solution {
public:
int rob(vector<int>& nums) {
int n=nums.size();
if(n==)return ;
if(n==)return nums[];
vector<int> maxV(n,);
maxV[]=nums[];
maxV[]=max(nums[],nums[]);
for(int i=;i<n;i++)maxV[i]=max(maxV[i-]+nums[i],maxV[i-]);
return maxV[n-];
}
};
【LeetCode】198 - House Robber的更多相关文章
- 【LeetCode】198. House Robber 打家劫舍 解题报告(Java & Python)
作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 递归 递归 + 记忆化 动态规划 优化动态规划空间 ...
- 【LeetCode】213. House Robber II 解题报告(Python)
作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 题目地址:https://leetcode.com/problems/house-rob ...
- 【刷题-LeetCode】198 House Robber
House Robber You are a professional robber planning to rob houses along a street. Each house has a c ...
- 【leetcode】198.HouseRobber
198.HouseRobber You are a professional robber planning to rob houses along a street. Each house has ...
- 【LeetCode】213. House Robber II
House Robber II Note: This is an extension of House Robber. After robbing those houses on that stree ...
- 【LeetCode】337. House Robber III 解题报告(Python)
作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 日期 题目地址:https://leetcode.c ...
- 【easy】198. House Robber 123总结……
题目一: 一个极其简单的动态规划. class Solution { public: int rob(vector<int>& nums) { ; // 表示没有选择当前house ...
- 【LeetCode】198. 打家劫舍
打家劫舍 你是一个专业的小偷,计划偷窃沿街的房屋.每间房内都藏有一定的现金,影响你偷窃的唯一制约因素就是相邻的房屋装有相互连通的防盗系统,如果两间相邻的房屋在同一晚上被小偷闯入,系统会自动报警. 给定 ...
- 【leetcode】337. House Robber III
The thief has found himself a new place for his thievery again. There is only one entrance to this a ...
随机推荐
- java Cache框架
Cache框架乱炖 各类开源的缓存解决方案 JBossCache/TreeCacheJBossCache是一个复制的事务处理缓存,它允许你缓存企业级应用数据来更好的改善性能.缓存数据被自动复制,让 ...
- wrong number of arguments,java方法反射时数组参数的坑
java方法中只有一个参数是数组,反射的时候我们不能想当然的传歌数组进去,传数组进去的时候表示多个参数. 两个数组不是一个意思啊. 我们应该把数组转为objet,这样才表示一个参数. import j ...
- 使用OPENROWSET(BULK...)从平面文件加载数据
要使用OPENROWSET首先要开启一个configure选项 sp_configure ‘show advanced options’, 1;GORECONFIGURE;GOsp_configure ...
- Test Tools
1. http://www.dummytextgenerator.com/: Generate dummy text 2. fsutil file createnew D:\New.txt 1024: ...
- android 从assets和res中读取文件
1. 相关文件夹介绍 在Android项目文件夹里面,主要的资源文件是放在res文件夹里面的.assets文件夹是存放不进行编译加工的原生文件,即该文件夹里面的文件不会像xml,java文件被预编译, ...
- 18.allegro区域约束规则设置
一.线宽和线间距 --- ---------------- 然后再电路板上创建一个区域 ----- ---- --- ---- ------------------------------
- Hosting Your Own NuGet Feeds
Hosting Your Own NuGet Feeds Hosting the NuGet Gallery Locally in IIS https://github.com/NuGet/NuGet ...
- 【Todo】各种排序整理
今天面试别人,问到堆排序.发现自己都记不太清楚了. 堆排序 从小到大排序,要用到的是,最大堆. 过程是最大堆,堆顶的最大的元素,调换到数组最后,依次进行.最后达到从小到大的效果. 归并排序 可以看这个 ...
- html_表单
一.表单框架简介 表单是提供让读者在网页上输入,勾选和选取数据,以便提交给服务器数据库的工具.(用于将信息提交给服务器) <form>...</form>标记 作用:用于创建一 ...
- CSS3与页面布局学习总结(三)——BFC、定位、浮动、垂直居中
一.BFC与IFC 1.1.BFC与IFC概要 BFC(Block Formatting Context)即“块级格式化上下文”, IFC(Inline Formatting Context)即行内格 ...