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的更多相关文章

  1. 【LeetCode 229】Majority Element II

    Given an integer array of size n, find all elements that appear more than ⌊ n/3 ⌋ times. The algorit ...

  2. 【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 ...

  3. 【LeetCode OJ】Word Ladder II

    Problem Link: http://oj.leetcode.com/problems/word-ladder-ii/ Basically, this problem is same to Wor ...

  4. 【LeetCode OJ】Palindrome Partitioning II

    Problem Link: http://oj.leetcode.com/problems/palindrome-partitioning-ii/ We solve this problem by u ...

  5. 【LEETCODE OJ】Single Number II

    Problem link: http://oj.leetcode.com/problems/single-number-ii/ The problem seems like the Single Nu ...

  6. 【LeetCode OJ】Word Break II

    Problem link: http://oj.leetcode.com/problems/word-break-ii/ This problem is some extension of the w ...

  7. 【LeetCode练习题】Unique Paths II

    Unique Paths II Follow up for "Unique Paths": Now consider if some obstacles are added to ...

  8. 【Leetcode 167】Two Sum II - Input array is sorted

    问题描述:给出一个升序排列好的整数数组,找出2个数,它们的和等于目标数.返回这两个数的下标(从1开始),其中第1个下标比第2个下标小. Input: numbers={2, 7, 11, 15}, t ...

  9. 【Leetcode链表】反转链表 II(92)

    题目 反转从位置 m 到 n 的链表.请使用一趟扫描完成反转. 说明: 1 ≤ m ≤ n ≤ 链表长度. 示例: 输入: 1->2->3->4->5->NULL, m ...

随机推荐

  1. C# 虚方法的重载 new 与 virtual

    using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.T ...

  2. nohup 程序名 & (使程序推到后台运行,即使终端关闭,该程序依然运行)

    IshallbeThatIshallbe:~ iamthat$ ps -ef |grep ping 502 450 1 0 9:30PM ?? 0:00.05 ping www.baidu.com 5 ...

  3. 【多媒体封装格式详解】---MKV

    http://blog.csdn.net/tx3344/article/details/8162656# http://blog.csdn.net/tx3344/article/details/817 ...

  4. JavaWeb项目开发案例精粹-第2章投票系统-005实体层

    1. package com.sanqing.bean; /** * * 投票选项类 * */ public class VoteOption { private int voteOptionID; ...

  5. java:异常处理

    异常:编译正常,但运行出错,会中断正常指令流 RuntimeException:运行时异常 分为:uncheck exception.和check exception(除了RuntimeExcepti ...

  6. Eclipse,IDEA自动生成相应对象接收方法返回值的快捷键

    @Service public class ItemServiceImpl implements ItemService { @Autowired private TbItemMapper itemM ...

  7. windows系统下Python环境的搭建

    1.下载最新的Python版本3.5.0.

  8. Filter登录验证过滤器(全局)

    通过Filter来定义一个登录验证过滤器,这是就不需要在每一个JSP页面添加判断用户合法性的代码了. 以下示例中包含了5个文件,一个是登录表单LoginForm.jsp,一个是登录判断页LoginCo ...

  9. parseInt和valueOf

    .parseInt和valueOf.split static int parseInt(String s) 将字符串参数作为有符号的十进制整数进行分析. static Integer valueOf( ...

  10. HDU 1710 Binary Tree Traversals

    题意:给出一颗二叉树的前序遍历和中序遍历,输出其后续遍历 首先知道中序遍历是左子树根右子树递归遍历的,所以只要找到根节点,就能够拆分出左右子树 前序遍历是按照根左子树右子树递归遍历的,那么可以找出这颗 ...