LeetCode之“动态规划”:House Robber && House Robber II
1. 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.
Credits:
Special thanks to @ifanchu for adding this problem and creating all test cases. Also thanks to @ts for adding additional test cases.
该题相当于从一个数组中取出一个或多个不相邻的数,使其和最大。具体代码如下:
class Solution {
public:
int rob(vector<int>& nums) {
int sz = nums.size();
int * dp = new int[sz];
for(int i = ; i < sz; i++)
{
if(i == )
dp[] = nums[];
else if(i == )
dp[] = max(nums[], nums[]);
else
dp[i] = max(nums[i] + dp[i - ], dp[i - ]);
}
return dp[sz - ];
}
};
2. 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.
Credits:
Special thanks to @Freezen for adding this problem and creating all test cases.
对于环形,主要考虑两种情况:
1) 第一个房子被偷:那么此时第二个房子和最后一个房子都不能被偷了,即我们可以从第三个房子到倒数最后一个房子之间用线性的动态规划求解。
2) 第一个房子没有被偷:那么此时我们可以从第二个房子到最后一个房子之间用线性的动态规划求解。
具体代码如下:
class Solution {
public:
int simpleRob(vector<int>& nums, int start, int end) {
int sz = end - start + ;
int * dp = new int[sz];
for(int i = ; i < sz; i++)
{
if(i == )
dp[] = nums[start];
else if(i == )
dp[] = max(nums[start + ], nums[start]);
else
dp[i] = max(nums[start + i] + dp[i - ], dp[i - ]);
}
return dp[sz - ];
}
int rob(vector<int>& nums) {
int sz = nums.size();
if(sz == )
return ;
else if(sz == )
return nums[];
else
return max(simpleRob(nums, , sz - ), simpleRob(nums, , sz - ));
}
};
LeetCode之“动态规划”:House Robber && House Robber II的更多相关文章
- Leetcode之动态规划(DP)专题-198. 打家劫舍(House Robber)
Leetcode之动态规划(DP)专题-198. 打家劫舍(House Robber) 你是一个专业的小偷,计划偷窃沿街的房屋.每间房内都藏有一定的现金,影响你偷窃的唯一制约因素就是相邻的房屋装有相互 ...
- Leetcode之动态规划(DP)专题-264. 丑数 II(Ugly Number II)
Leetcode之动态规划(DP)专题-264. 丑数 II(Ugly Number II) 编写一个程序,找出第 n 个丑数. 丑数就是只包含质因数 2, 3, 5 的正整数. 示例: 输入: n ...
- Leetcode之动态规划(DP)专题-122. 买卖股票的最佳时机 II(Best Time to Buy and Sell Stock II)
Leetcode之动态规划(DP)专题-122. 买卖股票的最佳时机 II(Best Time to Buy and Sell Stock II) 股票问题: 121. 买卖股票的最佳时机 122. ...
- Leetcode之动态规划(DP)专题-63. 不同路径 II(Unique Paths II)
Leetcode之动态规划(DP)专题-63. 不同路径 II(Unique Paths II) 初级题目:Leetcode之动态规划(DP)专题-62. 不同路径(Unique Paths) 一个机 ...
- Leetcode之动态规划(DP)专题-121. 买卖股票的最佳时机(Best Time to Buy and Sell Stock)
Leetcode之动态规划(DP)专题-121. 买卖股票的最佳时机(Best Time to Buy and Sell Stock) 股票问题: 121. 买卖股票的最佳时机 122. 买卖股票的最 ...
- Leetcode之动态规划(DP)专题-123. 买卖股票的最佳时机 III(Best Time to Buy and Sell Stock III)
Leetcode之动态规划(DP)专题-123. 买卖股票的最佳时机 III(Best Time to Buy and Sell Stock III) 股票问题: 121. 买卖股票的最佳时机 122 ...
- Leetcode之动态规划(DP)专题-188. 买卖股票的最佳时机 IV(Best Time to Buy and Sell Stock IV)
Leetcode之动态规划(DP)专题-188. 买卖股票的最佳时机 IV(Best Time to Buy and Sell Stock IV) 股票问题: 121. 买卖股票的最佳时机 122. ...
- Leetcode之动态规划(DP)专题-309. 最佳买卖股票时机含冷冻期(Best Time to Buy and Sell Stock with Cooldown)
Leetcode之动态规划(DP)专题-309. 最佳买卖股票时机含冷冻期(Best Time to Buy and Sell Stock with Cooldown) 股票问题: 121. 买卖股票 ...
- Leetcode之动态规划(DP)专题-714. 买卖股票的最佳时机含手续费(Best Time to Buy and Sell Stock with Transaction Fee)
Leetcode之动态规划(DP)专题-714. 买卖股票的最佳时机含手续费(Best Time to Buy and Sell Stock with Transaction Fee) 股票问题: 1 ...
- LeetCode 81 Search in Rotated Sorted Array II [binary search] <c++>
LeetCode 81 Search in Rotated Sorted Array II [binary search] <c++> 给出排序好的一维有重复元素的数组,随机取一个位置断开 ...
随机推荐
- java创建线程
创建一个线程 Java提供了两种创建线程方法: 通过实现Runable接口: http://blog.csdn.net/duruiqi_fx/article/details/52187275 通过继承 ...
- linux简单命常用令
Linux常用命令总结 切换:cd tmp cd/tmp/yun cd 切换到host目录 cd .. 显示:ll Top显示系统情况 Netstat显示网络情况 Ifconfig显示网络配置 Mor ...
- 使用github搭建网站
http://blog.csdn.net/pipisorry/article/details/51707366 使用github建站 github设计了Pages功能,允许用户自定义项目首页,用来替代 ...
- FFmpeg源代码简单分析:av_write_trailer()
===================================================== FFmpeg的库函数源代码分析文章列表: [架构图] FFmpeg源代码结构图 - 解码 F ...
- XML之DTD(文档类型定义)
文档类型定义(DTD)可定义合法的XML文档构建模块.它使用一系列合法的元素来定义文档的结构. DTD 可被成行地声明于 XML 文档中,也可作为一个外部引用. 声明元素 在 DTD 中,XML 元素 ...
- 【java集合框架源码剖析系列】java源码剖析之java集合中的折半插入排序算法
注:关于排序算法,博主写过[数据结构排序算法系列]数据结构八大排序算法,基本上把所有的排序算法都详细的讲解过,而之所以单独将java集合中的排序算法拿出来讲解,是因为在阿里巴巴内推面试的时候面试官问过 ...
- Android初级教程理论知识(第七章服务)
服务两种启动方式 startService:服务被启动之后,跟启动它的组件没有一毛钱关系 bindService:跟启动它的组件同生共死 绑定服务和解绑服务的生命周期方法:onCreate->o ...
- C语言--static修饰变量
Static在C语言里面有两个作用,第一个是修饰变量,第二个是修饰函数. 1.Static修饰变量 按照作用范围的不同,变量分为局部变量和全局变量.如果用static修饰变量,不论这个变量是全局的还是 ...
- Cocos2D iOS之旅:如何写一个敲地鼠游戏(六):放置地鼠
大熊猫猪·侯佩原创或翻译作品.欢迎转载,转载请注明出处. 如果觉得写的不好请告诉我,如果觉得不错请多多支持点赞.谢谢! hopy ;) 免责申明:本博客提供的所有翻译文章原稿均来自互联网,仅供学习交流 ...
- 通过邮件找回密码功能的Java实现
1.有个需求就是,忘记密码后通过邮箱找回.现在的系统在注册的时候都会强制输入邮箱,其一目的就是 通过邮件绑定找回,可以进行密码找回.通过java发送邮件的功能我就不说了,重点讲找回密码. 2.参考别人 ...