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. min-device-pixel-ratio

    Devices with -webkit-min-device-pixel-ratio: 2.0 All Macs with Retina displaysApple iPhone 4Apple iP ...

  2. 解决pydev报unsolved import的问题

    安装Flask_RESTful-0.2.11包后, 并在pydev 对应的 interpreter 重新刷新了System PYTHONPATH, 看见Lib\site-packages\flask_ ...

  3. 导入 sun.net.TelnetInputStream; 报错

    // 对于导入 sun.net.TelnetInputStream; 报错 是权限不足 myeclise 默认不是使用sun 下面的额工具类 也可以自己建立一个 TelnetInputStream 继 ...

  4. .net的一些新语法的整理

    using System; using System.Collections.Generic; using System.IO; using System.Linq; using System.Tex ...

  5. YC大牛的判题任务-想法

    YC大牛的判题任务 Time Limit: 1000ms Memory Limit: 65536KB   64-bit integer IO format: %lld      Java class ...

  6. 【转】phpcms-v9中关于模型的理解

    PHPCMS v9 模型概念 一.什么是模型? 模型是系统知识的抽象表示.我们不能仅仅通过语言来描述一个系统,也不能仅仅通过记忆来记录关于系统的知识.知识是通过某种媒介来表达的,这种媒介所表达的内容就 ...

  7. 跟着百度学PHP[4]OOP面对对象编程-12-对象接口技术(interface)

    PHP与大多数面向对象编程语言一样,不支持多重继承.也就是说每个类只能继承一个父类. 接口正是解决每个类只能继承一个父类这个问题的 接口用什么权限,继承的那个方法也要使用什么权限. 接口的声明使用:i ...

  8. HDU 1159 裸最长公共子串

    试着拍了一道模板题 dp开了500,开100会超时..... string类型中间有空格会判为结束 #include<algorithm> -->min,max函数的头文件 #inc ...

  9. dp题目列表

    此文转载别人,希望自己能够做完这些题目! 1.POJ动态规划题目列表 容易:1018, 1050, 1083, 1088, 1125, 1143, 1157, 1163, 1178, 1179, 11 ...

  10. django ATOMIC_REQUESTS

    在数据库配置中,如果配置了此属性为True,如下: DATABASES = { 'default': { 'ENGINE': 'django.db.backends.xxx', 'NAME': '', ...