【leetcode】House Robber & House Robber II(middle)
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)的更多相关文章
- 【leetcode】Pascal's Triangle I & II (middle)
Given numRows, generate the first numRows of Pascal's triangle. For example, given numRows = 5,Retur ...
- 【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 ...
- 【leetcode】Bitwise AND of Numbers Range(middle)
Given a range [m, n] where 0 <= m <= n <= 2147483647, return the bitwise AND of all numbers ...
- 【leetcode】Longest Substring Without Repeating Characters (middle)
Given a string, find the length of the longest substring without repeating characters. For example, ...
- 【LeetCode】385. Mini Parser 解题报告(Python)
[LeetCode]385. Mini Parser 解题报告(Python) 标签: LeetCode 题目地址:https://leetcode.com/problems/mini-parser/ ...
- 【LeetCode】376. Wiggle Subsequence 解题报告(Python)
[LeetCode]376. Wiggle Subsequence 解题报告(Python) 作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.c ...
- 【LeetCode】649. Dota2 Senate 解题报告(Python)
[LeetCode]649. Dota2 Senate 解题报告(Python) 作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 题目地 ...
- 【LeetCode】911. Online Election 解题报告(Python)
[LeetCode]911. Online Election 解题报告(Python) 作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ ...
- 【LeetCode】886. Possible Bipartition 解题报告(Python)
[LeetCode]886. Possible Bipartition 解题报告(Python) 作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu ...
- 【LeetCode】36. Valid Sudoku 解题报告(Python)
[LeetCode]36. Valid Sudoku 解题报告(Python) 作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 题目地址 ...
随机推荐
- [译]bare repository
git init --bare 使用--bare创建的repository没有工作目录, 在这个repository中不能修改文件和commit. 中心repository必须是bare reposi ...
- 解决英文或数字在HTMl网页中不自动换行。
对于网页设计的新手而言,在接触一段时间的HTML/CSS后,一定会遇到这样的问题:对于已经定义了宽度的容器(如DIV,TD,段落等)如果里面出现了较长的英文或数字,则内容不能自动换行然后会将框架撑出设 ...
- Linux统计文件行数
语法:wc [选项] 文件… 说明:该命令统计给定文件中的字节数.字数.行数.如果没有给出文件名,则从标准输入读取.wc同时也给出所有指定文件的总统计数.字是由空格字符区分开的最大字符串. 该命令各选 ...
- XDU 1161 - 科协的数字游戏II
Problem 1161 - 科协的数字游戏II Time Limit: 1000MS Memory Limit: 65536KB Difficulty: Total Submit: 112 ...
- android版微信5.2.1更新 支持微信聊天记录备份到电脑上
昨天微信 5.2.1 for Android 全新发布了,和微信 5.2.1 for iPhone一样,支持拍照分享,可以把照片发送给多个朋友,最重要的一个更新是支持微信聊天记录备份到电脑(可以通过腾 ...
- bug--java访问hdfs (Server IPC version 9 cannot communicate with client version 4 错误)
1. 今天想做一个hdfs的java工具类,但是在连接hdfs的时候,报如下错误: Exception in thread "main" org.apache.hadoop.ipc ...
- C++调用shell
1.直接采用system() 2.popen http://www.cnblogs.com/xitang/p/4288808.html
- php运行出现Call to undefined function curl_init()的解决方法
解决方法如下: 1.在php.ini中找到extension=php_curl.dll,去掉前面的分号;,然后将php.ini拷贝到c:\windows. 2.重启IIS服务,或回收应用程序池即可.
- 关于sql用<>不等于查询数据不对问题
平常查询数据 ' 当想要查询 不等于1 的数据的时候,一般会这样查询 ' 此处查询结果没查到所有想要的结果,如果night_flag 列数据为 null时,此行数据是查询不到的. 解决方法一: ' 解 ...
- 关于js css html加载顺序整理
1.js放在head中会立即执行,阻塞后续的资源下载与执行.因为js有可能会修改dom,如果不阻塞后续的资源下载,dom的操作顺序不可控. 正常的网页加载流程是这样的. 浏览器一边下载HTML网页,一 ...