【LeetCode 213】House Robber II
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.
思路:
此题为House Robber(简单dp)的拓展版本,将之前一条笔直的街道改为了一条首位相连的环状街道。这就增加了一种情况:若强盗决定洗劫第一间房子,那么他就不能洗劫最后一间房子了(环嘛,首位相连的),若他不洗劫第一间房子,那么后面的房子就可以随意洗劫了。
class Solution {
public:
int houseNum;//房子总数
int smartRob(vector<int>& houses, const int start, const int stop)
{
//初始化dp数组及前2个房子的信息
vector<int> dp(houseNum, );
dp[] = houses[];
dp[] = houses[];
//两种情况,视start和stop的值确定,返回结果为当前洗劫模式下最大获利
//若start == 1,则洗劫[0, houseNum-1]范围内的房间
//若start == 2,则洗劫[1, houseNum]范围内的房间
for(int i = start; i < stop; i++)
dp[i] = (i == start) ? max(dp[i-], houses[i]):max(dp[i-], dp[i-] + houses[i]);
return max(dp[houseNum - ], dp[houseNum - ]);
}
int rob(vector<int>& nums) {
houseNum = nums.size();
//两种特殊的情况,没有房子和只有一间房子
if(houseNum == )
return ;
if(houseNum == )
return nums[];
//返回两种可能的洗劫方式中值最大的一个就是所求结果
return max(smartRob(nums, , houseNum - ), smartRob(nums, , houseNum));
}
};
【LeetCode 213】House Robber II的更多相关文章
- 【LeetCode 229】Majority Element II
Given an integer array of size n, find all elements that appear more than ⌊ n/3 ⌋ times. The algorit ...
- 【LeetCode OJ】Path Sum II
Problem Link: http://oj.leetcode.com/problems/path-sum-ii/ The basic idea here is same to that of Pa ...
- 【LeetCode OJ】Word Ladder II
Problem Link: http://oj.leetcode.com/problems/word-ladder-ii/ Basically, this problem is same to Wor ...
- 【LeetCode OJ】Palindrome Partitioning II
Problem Link: http://oj.leetcode.com/problems/palindrome-partitioning-ii/ We solve this problem by u ...
- 【LEETCODE OJ】Single Number II
Problem link: http://oj.leetcode.com/problems/single-number-ii/ The problem seems like the Single Nu ...
- 【LeetCode OJ】Word Break II
Problem link: http://oj.leetcode.com/problems/word-break-ii/ This problem is some extension of the w ...
- 【LeetCode练习题】Unique Paths II
Unique Paths II Follow up for "Unique Paths": Now consider if some obstacles are added to ...
- 【Leetcode 167】Two Sum II - Input array is sorted
问题描述:给出一个升序排列好的整数数组,找出2个数,它们的和等于目标数.返回这两个数的下标(从1开始),其中第1个下标比第2个下标小. Input: numbers={2, 7, 11, 15}, t ...
- 【Leetcode链表】反转链表 II(92)
题目 反转从位置 m 到 n 的链表.请使用一趟扫描完成反转. 说明: 1 ≤ m ≤ n ≤ 链表长度. 示例: 输入: 1->2->3->4->5->NULL, m ...
随机推荐
- P1024 外星人的密码数字
P1024 外星人的密码数字 时间: 1000ms / 空间: 131072KiB / Java类名: Main 描述 XXXX年突然有外星人造访,但大家语言不通,不过科学家们经过研究发现外星 ...
- ios开发--清理缓存
ios文章原文 一段清理缓存的代码如下: dispatch_async( dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0) , ...
- kali 安装中文输入法
(1)apt-get install fcitx-googlepinyin (2)你希望继续执行吗?Y (3)连续执行数次 (4)init 6(命令行重启)
- DefaultHashOperations multiget的一个坑
DefaultHashOperations的multiget如果没有数据会返回java.util.Collections.EmptyList,这个List没有重写add方法. List<Long ...
- [置顶] android 与JavaScript的互相调用
1.html代码 <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN"> <html> ...
- 车牌识别LPR(七)-- 字符特征
第七篇:字符特征 选择的字符特征应该满足以下条件: (1)选取的字符特征具有较强的鲁棒性,不受字符变形.弯曲等影响. (2)两个字符的字符特征不能完全相同,但部分相同是允许的,即选择的字符特征是唯一的 ...
- ubuntu13.04下载android4.0.1源码过程
最初我参考的是老罗的博客http://blog.csdn.net/luoshengyang/article/details/6559955 进行下载安装的,但弄着弄着就发现不太对劲了.这里记录下详细过 ...
- 在Ubuntu上为Android系统内置C可执行程序测试Linux内核驱动程序(老罗学习笔记2)
在前一篇文章中,我们介绍了如何在Ubuntu上为Android系统编写Linux内核驱动程序.在这个名为hello的Linux内核驱动程序中,创建三个不同的文件节点来供用户空间访问,分别是传统的设备文 ...
- Android上常见度量单位【xdpi、hdpi、mdpi、ldpi】解读
术语和概念 屏幕尺寸 屏幕的物理尺寸,以屏幕的对角线长度作为依据(比如 2.8寸, 3.5寸). 简而言之, Android把所有的屏幕尺寸简化为三大类:大,正常,和小. 程序可以针对这三种尺 ...
- 转:Cache相关
声明:本文截取自http://blog.163.com/ac_victory/blog/static/1033187262010325113928577/ (1)“Cache”是什么 Cache(即高 ...