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. ubuntu -LDAP的配置

    本文内容来自 http://blog.csdn.net/jl19861101/article/details/5582841 1. LDAP Server1.1. 安装主要安装一下套件: 代码: # ...

  2. 李洪强iOS开发之【零基础学习iOS开发】【01-前言】02-准备

    在上一讲中,介绍了什么是iOS开发.说简单一点,iOS开发,就是开发运行在iPhone或者iPad上的软件.这么一说完,应该有很多人就会产生一些疑惑,比如学习iOS开发是不是一定要买iPhone?需不 ...

  3. 银联接口(注意项&备忘)

    1,参考文档“证书下载.导出及上传流程.docx” 按照文档上所述,依次进行,导出的证书备用,用于配置文件的项“const SDK_ENCRYPT_CERT_PATH” 2,使用tp框架 新建一个控制 ...

  4. C#调用Win32 api学习总结

    从.NET平台调用Win32 API Win32 API可以直接控制Microsoft Windows的核心,因为API(Application Programming Interface)本来就是微 ...

  5. 3.Spring-用反射模拟IoC

    1.BeanFactory.java package com.jike.spring.chapter03.reflect; import java.io.InputStream; import jav ...

  6. Retrofit分析-漂亮的解耦套路

    没耐心自己分析源码的同学,还可以参考Stay录制的视频版 Retrofit分析-漂亮的解耦套路(视频版) 万万没想到Retrofit会这么火,在没看源码之前,我简单的认为是因为它跟OkHttp同出一源 ...

  7. 机器人学 —— 轨迹规划(Introduction)

    轨迹规划属于机器人学中的上层问题,其主要目标是计划机器人从A移动到B并避开所有障碍的路线. 1.轨迹计划的对象 轨迹规划的对象是map,机器人通过SLAM获得地map后,则可在地图中选定任意两点进行轨 ...

  8. ubuntu下android源码下载

    步骤一: 首先保证你的ubuntu系统电脑可以顺利游览google,我们是将etc下 hosts替换掉,推荐hosts: http://laod.cn/hosts/2015-google...host ...

  9. Pod::Executable pull

    使用cocoapods 的时候遇到了以下错误:[!] Pod::Executable pull Updating eaf98af..ba3c030 error: Your local changes ...

  10. abstract的method是否可同时是static,是否可同时是native,是否可同时是synchronized?

    abstract的method不可以是static的,因为抽象的方法是要被子类实现的,而static与子类扯不上关系! native方法表示该方法要用另外一种依赖平台的编程语言实现的,不存在着被子类实 ...