198. House Robber,213. House Robber II
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.
- class Solution {
- public:
- int rob(vector<int>& nums) {
- int nums_size = nums.size();
- int max_money = ;
- vector<int>dp(nums_size,);
- for(int i=;i<nums_size;++i){
- int m = ;
- for(int j=i-;j>=;j--){
- m = max(m,dp[j]);
- }
- dp[i] = m+nums[i];
- max_money = max(max_money,dp[i]);
- }
- return max_money;
- }
- };
- /**
- dp[i] = max(dp[i-2],dp[i-3]...dp[1])+nums[i];
- [9,8,9,20,8]
- [1,2,3,55,54,2]
- */
213. House Robber II
Note: This is an extension of House Robber.
After robbing those houses on that street, the thief has found himself a new place for his thievery so that he will not get too much attention. This time, all houses at this place are arranged in a circle. That means the first house is the neighbor of the last one. Meanwhile, the security system for these houses remain the same as for those in the previous street.
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[i] = max(dp[i-2],dp[i-3]...dp[1])+nums[i];
- [9,8,9,20,8]
- [1,2,3,55,54,2]
- */
- class Solution {
- public:
- int rob(vector<int>& nums,int start,int end) {
- if(end<=start) return ;
- int nums_size = end-start;
- int max_money = ;
- vector<int>dp(nums_size,);
- int k = ;
- cout<<"start="<<start<<" end="<<endl;
- for(int i=start;i<end;++i){
- int m = ;
- for(int j=k-;j>=;j--){
- m = max(m,dp[j]);
- }
- dp[k] = m+nums[i];
- cout<<"dp["<<(k)<<"]="<<dp[k]<<endl;
- max_money = max(max_money,dp[k]);
- k++;
- }
- return max_money;
- }
- int rob(vector<int>& nums) {
- int nums_size = nums.size();
- return nums_size== ? nums[] : max(rob(nums,,nums_size-),rob(nums,,nums_size));
- }
- };
198. House Robber,213. House Robber II的更多相关文章
- 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】213. House Robber II
House Robber II Note: This is an extension of House Robber. After robbing those houses on that stree ...
- [LeetCode] 213. House Robber II 打家劫舍之二
You are a professional robber planning to rob houses along a street. Each house has a certain amount ...
- [LeetCode] 213. House Robber II 打家劫舍 II
Note: This is an extension of House Robber. After robbing those houses on that street, the thief has ...
- 【LeetCode】213. House Robber II 解题报告(Python)
作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 题目地址:https://leetcode.com/problems/house-rob ...
- 【刷题-LeetCode】213. House Robber II
House Robber II You are a professional robber planning to rob houses along a street. Each house has ...
- Java for LeetCode 213 House Robber II
Note: This is an extension of House Robber. After robbing those houses on that street, the thief has ...
- 213. House Robber II
题目: Note: This is an extension of House Robber. After robbing those houses on that street, the thief ...
- LeetCode 213. House Robber II
Note: This is an extension of House Robber. After robbing those houses on that street, the thief has ...
随机推荐
- oracle用户权限的问题
一.创建用户 create user username identified by password --username 创建的用户的名称 --password 创建的用户的密码 二.赋权限 gra ...
- JSP脚本元素上机手册
L3 <JSP基础>上机手册 内容回顾 脚本元素<%! %> <%= %> <% %> 注释元素 JSP指令元素 JSP动作元素 上机目标 掌握脚本元素 ...
- 值栈和OGNL 之 7.1 值栈
7.1 值栈 7.1.1 值栈是什么 简单的说:值栈是对应每一个请求对象的轻量级的内存数据中心. Struts2中一个很激动人心的特性就是引入了值栈,在这里统一管理着数据,供Action.Resu ...
- C++服务器设计(二):应用层I/O缓冲
数据完整性讨论 我们已经选择了I/O复用模型作为系统底层I/O模型.但是我们并没有具体解决读写问题,即在我们的Reactor模式中,我们怎么进行读写操作,才能保证对于每个连接的发送或接收的数据是完整的 ...
- c++设计模式之策略模式
概念:通过定义一系列封装的算法,使得调度者可以互相替换,此模式让算法的变化,不会影响到使用算法的客户. 特点: 1)根据不同的情况创建不同的对象. 2)每个对象的方法名相同,但实现却不同. 结构: 1 ...
- QT TCP/IP
QT 网络通信(TCP/IP) 服务端: 一.监听新的客户端接入(QTcpServer) 重写函数 incomingConnection(qintptr socketDescriptor) 二.服务端 ...
- javascript统计输入文本的简易方法
计算文本框的输入字符数的简易方法: ]; var tValue = text.value; num = Math.ceil(getLength(tValue)/); //正则:用于区分中文为两个字节 ...
- [C++程序设计]内置函数
注意: 可以在声明函数和定义函数时同时写 inline,也可以只在其中一处声明inline,效果相同,都能按内置函数处理. 使用内置函数可以节省运行时间,但却增加了目标 程序的长度.因此一般只将规模很 ...
- ASP.NET MVC 4.0 学习5-ActionResult
一,Controller簡介 Controller擔任了資料傳遞的角色,負責流程控制,決定存取哪個Model以及決定顯示哪個View頁面,即ASP.NET MVC中有關於『傳遞』的任務皆由Contro ...
- Keil UV4 BUG(带字库液晶不能显示“数、正、过”问题的请看)
Keil UV3一直存在汉字显示(0xFD)的bug,以前在用到带字库的12864液晶的时候,“数”字总是不能正常显示,后来有网友告诉我这是keil的bug,解决掉了.后来keil升级了,我也换了新版 ...