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. mac 设置 ll 等alias 并永久生效

    往上看了在.bash_profile中配置 然后 source  的方法, 试过了, 只是当前的终端有效,当电脑重启或者关闭终端就失效了,只好看看 mac 的 profile 代码 # System- ...

  2. Python Snippet

    python按行读取文件,如何去掉换行符"\n" for line in file.readlines(): line=line.strip('\n') python没有subst ...

  3. 安装eclipse for JavaEE 后的一些设置

    以下的设置是相对于一个workspace而设置的,如果更换了workspace则要重新设置. 1. 设置Text Editors: 2. 设置Content Assist 的快捷键(比较方便) 3. ...

  4. 对于linux下system()函数的深度理解(整理)

    原谅: http://blog.sina.com.cn/s/blog_8043547601017qk0.html 这几天调程序(嵌入式linux),发现程序有时就莫名其妙的死掉,每次都定位在程序中不同 ...

  5. DelphiXE下String转PAnsiChar(反向转换)

    很多资料只提到升迁到xe,而我们调用底版本c++开发的程序,是只能按Ansi操作的,所以需要反向转换. var s:PansiChar;s:=PansiChar(AnsiString('我我我我我') ...

  6. ConcurrentHashMap使用示例

    ConcurrentHashMap使用示例 发表于2年前(2013-07-12 19:05)   阅读(3660) | 评论(0) 25人收藏此文章, 我要收藏 赞5 如何快速提高你的薪资?-实力拍“ ...

  7. PCL—低层次视觉—点云分割(最小割算法)

    1.点云分割的精度 在之前的两个章节里介绍了基于采样一致的点云分割和基于临近搜索的点云分割算法.基于采样一致的点云分割算法显然是意识流的,它只能割出大概的点云(可能是杯子的一部分,但杯把儿肯定没分割出 ...

  8. 常用的coco2d-x游戏开发工具(转)

    物理编辑工具Physics Editing ToolsMekanimo 网址:http://www.mekanimo.net/PhysicsBench 网址:http://www.cocos2d-ip ...

  9. Hibernate--基本映射标签和属性介绍

    一.映射文件的基本结构举例: <?xml version="1.0" encoding="UTF-8"?> <!DOCTYPE hiberna ...

  10. C#获取ip的示例

    界面 using System;using System.Collections.Generic;using System.ComponentModel;using System.Data;using ...