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.

思路:开始写了个递归超时了。反应了好久才反应过来要用动态规划。

递归代码:

int rob(vector<int> &num) {
return robsub(num, , num.size());
}
int robsub(vector<int> &num, int s, int e)
{
if(s == e) return ;
if(e - s == ) return num[s];
return max(robsub(num, s + , e), num[s] + ((e - s >= ) ? robsub(num, s + , e) : ));
}

动态规划:

int rob2(vector<int> &num) {
vector<int> dp(num.size() + , ); //截止到num[i] - 1时的最大值
int ans = ;
for(int i = ; i <= num.size(); i++)
{
dp[i] = num[i - ] + max(((i - ) >= ? dp[i - ] : ), ((i - ) >= ? dp[i - ] : ));
ans = (ans > dp[i]) ? ans : dp[i];
}
return ans;
}

我的动态规划代码用的空间太多了,其实只要两个变量记录一下前面的就好。

public class Solution {
public int rob(int[] num) {
int i = ;
int e = ;
for (int k = ; k<num.length; k++) {
int tmp = i;
i = num[k] + e;
e = Math.max(tmp, e);
}
return Math.max(i,e);
}
}

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.

思路:

时隔一个月做2,结果还是写递归。又是反应好久才想起来是动态规划。失败啊....

连成一个圈了,我们可以把问题分解为从房屋0到n-2和房屋1到n-1两个部分。

最优代码,dp,节约空间

int rob2(vector<int>& nums) {
if(nums.empty()) return ;
if(nums.size() == ) return nums[];
int maxMoney = ;
int pre = , cur = ;
//抢第一间房屋 不抢最后一间房屋的情况
for(int i = ; i < nums.size() - ; ++i)
{
int tmp = cur;
cur = nums[i] + pre;
pre = max(tmp, pre); //取前一个值和前前一个值中较大的
}
maxMoney = max(cur, pre);
//不抢第一间房屋 抢最后一间的情况
cur = pre = ;
for(int i = ; i < nums.size(); ++i)
{
int tmp = cur;
cur = nums[i] + pre;
pre = max(tmp, pre);
}
maxMoney = max(maxMoney, max(cur, pre));
return maxMoney;
}

次优代码,dp, 数组存储

int rob(vector<int>& nums) {
if(nums.empty()) return ;
if(nums.size() == ) return nums[];
int maxMoney = ;
vector<int> dp1(nums.size(), );
vector<int> dp2(nums.size(), );
//抢第一间房屋的情况
for(int i = ; i < nums.size() - ; ++i)
{
dp1[i] = nums[i] + max((i - >= ) ? dp1[i - ] : , (i - >= ) ? dp1[i - ] : );
maxMoney = (dp1[i] > maxMoney) ? dp1[i] : maxMoney;
}
//不抢第一间房屋的情况
for(int i = ; i < nums.size(); ++i)
{
dp2[i] = nums[i] + max((i - >= ) ? dp2[i - ] : , (i - >= ) ? dp2[i - ] : );
maxMoney = (dp2[i] > maxMoney) ? dp2[i] : maxMoney;
}
return maxMoney;
}

最烂的递归代码,超时。

//递归 超时
int rob1(vector<int>& nums) {
if(nums.empty()) return ;
int maxMoney = ;
int curMoney = ;
recursion(maxMoney, curMoney, , nums, false);
curMoney = nums[];
recursion(maxMoney, curMoney, , nums, true);
return maxMoney;
} void recursion(int &maxMoney, int &curMoney, int id, vector<int>& nums, bool isFirstUsed)
{
if(id >= nums.size())
{
maxMoney = (curMoney > maxMoney) ? curMoney : maxMoney;
}
else if(id == nums.size() - && isFirstUsed) //当前是最后一个屋子 但第一个屋子抢过
{
recursion(maxMoney, curMoney, id + , nums, isFirstUsed); //不抢当前房屋
}
else
{
int money = nums[id];
recursion(maxMoney, curMoney, id + , nums, isFirstUsed); //不抢当前房屋
curMoney += money; //抢当前房屋
recursion(maxMoney, curMoney, id + , nums, isFirstUsed);
curMoney -= money;
}
}

【leetcode】House Robber & House Robber II(middle)的更多相关文章

  1. 【leetcode】Pascal's Triangle I & II (middle)

    Given numRows, generate the first numRows of Pascal's triangle. For example, given numRows = 5,Retur ...

  2. 【leetcode】Binary Tree Right Side View(middle)

    Given a binary tree, imagine yourself standing on the right side of it, return the values of the nod ...

  3. 【leetcode】Bitwise AND of Numbers Range(middle)

    Given a range [m, n] where 0 <= m <= n <= 2147483647, return the bitwise AND of all numbers ...

  4. 【leetcode】Longest Substring Without Repeating Characters (middle)

    Given a string, find the length of the longest substring without repeating characters. For example, ...

  5. 【LeetCode】385. Mini Parser 解题报告(Python)

    [LeetCode]385. Mini Parser 解题报告(Python) 标签: LeetCode 题目地址:https://leetcode.com/problems/mini-parser/ ...

  6. 【LeetCode】376. Wiggle Subsequence 解题报告(Python)

    [LeetCode]376. Wiggle Subsequence 解题报告(Python) 作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.c ...

  7. 【LeetCode】649. Dota2 Senate 解题报告(Python)

    [LeetCode]649. Dota2 Senate 解题报告(Python) 作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 题目地 ...

  8. 【LeetCode】911. Online Election 解题报告(Python)

    [LeetCode]911. Online Election 解题报告(Python) 作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ ...

  9. 【LeetCode】886. Possible Bipartition 解题报告(Python)

    [LeetCode]886. Possible Bipartition 解题报告(Python) 作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu ...

  10. 【LeetCode】36. Valid Sudoku 解题报告(Python)

    [LeetCode]36. Valid Sudoku 解题报告(Python) 作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 题目地址 ...

随机推荐

  1. 解决python3 UnicodeEncodeError: 'gbk' codec can't encode character '\xXX' in position XX

    从网上抓了一些字节流,想打印出来结果发生了一下错误: UnicodeEncodeError: 'gbk' codec can't encode character '\xbb' in position ...

  2. 【MongoDB】MongoDb的“not master and slaveok=false”错误及解决方法

    链接mongodb报错如下 2016-03-14T16:26:00.912+0800 E QUERY [thread1] Error: listDatabases failed:{ "ok& ...

  3. 基于iSCSI的SQL Server 2012群集测试(五)--镜像,作业,复制分发测试

    7.1.镜像测试 群集可以正常镜像到非群集环境,本次测试采用,无见证服务器的sql server验证的镜像连接,不同的是群集环境的镜像IP是采用SQL Server虚拟IP进行通信连接. 群集服务器: ...

  4. JS/HTML 保存图片到本地:HTML <a> download 属性

    JS如何保存图片到本地呢?自己百度一下吧! 这里想要说的是,可以利用 HTML 的 <a> 标签 来是实现保存图片到本地的功能,参考代码如下: <a href="http: ...

  5. Java小程序--抓取emai

    一.实现思路 1.使用Java.net.URL对象,绑定网络上某一个网页的地址 2.通过java.net.URL对象的openConnection()方法获得一个HttpConnection对象 3. ...

  6. 目前主流的Android定位有如下几种:

    1.通过GPS模块 GPS方式准确度是最高的,但是它的缺点也非常明显:1,比较耗电:2,绝大部分用户默认不开启GPS模块:3,从GPS模块启动到获取第一次定位数据,可能需要比较长的时间:4,室内几乎无 ...

  7. cocos2dx中CC_CALLBACK_1等宏中this指针实际指向

    首先看代码,我在Helloworld中添加两个函数. void HelloWorld::addTarget(){ Size visibleSize = Director::getInstance()- ...

  8. HDU 5073 Galaxy(2014鞍山赛区现场赛D题)

    题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=5073 解题报告:在一条直线上有n颗星星,一开始这n颗星星绕着重心转,现在我们可以把其中的任意k颗星星移 ...

  9. BZOJ2049——[Sdoi2008]Cave 洞穴勘测

    1.题目大意:就是一个动态维护森林联通性的题 2.分析:lct模板题 #include <stack> #include <cstdio> #include <cstdl ...

  10. Qt5 任务栏托盘功能实现

    23333 有一阵子没写博客了,研究了挺长时间qt,学到任务栏托盘时简直无语,网上找得到的代码大多是废码,Qt5不支持或者本身就有毛病不能实现却被n多人转来转去的,甚是无语. 简单托盘功能以下在Qt5 ...