213. House Robber II

 
 
Total Accepted: 24216 Total Submissions: 80632 Difficulty: Medium

Note: 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.

Credits:
Special thanks to @Freezen for adding this problem and creating all test cases.

Subscribe to see which companies asked this question

这个题目是基于House Robber I 的,所以做这题之前要先知道HouseRobber I的解法。

House Robber I 的传送门:

House Robber I

如果已经ac了第一题,那么这题的意思就是把屋子都改成环状。

在第一题中,dp状态转移方程    max[ i ] = Math.max( max[ i - 1 ], nums[ i - 1 ] + max[ i - 2 ] ) 已经做出来了。那么第二题就很好做了。

第二题中,我的做法就是要再进行一次dp,并且需要记录下选择屋子的首尾,记为start和last.

在记录start的时候,要注意start的状态 : 1.当前面的屋子已经被选择。2.当前面的屋子没有被选择。

所以这里的dp转移方程总结为:

            if( max[ i - 1 ] > nums[ i - 1 ] + max[ i - 2 ] ) {
max[ i ] = max[ i - 1 ];
start[ i ] = start[ i - 1 ];
} else {
max[ i ] = nums[ i - 1 ] + max[ i - 2 ];
last = i;
if( max[ i - 1 ] == max[ i - 2 ] ) {
start[ i ] = start[ i - 2 ];
} else {
start[ i ] = start[ i - 2 ] == 0 ? 2 : start[ i - 2 ];
}
}

最后判断一下,如果是选择最后一个的时候报警了,进行判断是要选择(1,n)还是(0,n-1)的最大价值

总的思想就是进行两次DP,(1,n)和(0,n-1)分别Dp

Ps:暂时没有想到更好更加简洁的方法。不过我觉得是有的,只是本人愚笨没想到

public class Solution {

    public int rob( int[] nums ) {

        int[] max = new int[ nums.length + 1 ];
int[] start = new int[ nums.length + 1 ];
max[ 0 ] = 0;
start[ 0 ] = 0;
if( nums == null || nums.length == 0 )
return 0;
max[ 1 ] = nums[ 0 ];
start[ 1 ] = 1;
int last = 1;
for( int i = 2; i <= nums.length; i++ ) {
if( max[ i - 1 ] > nums[ i - 1 ] + max[ i - 2 ] ) {
max[ i ] = max[ i - 1 ];
start[ i ] = start[ i - 1 ];
} else {
max[ i ] = nums[ i - 1 ] + max[ i - 2 ];
last = i;
if( max[ i - 1 ] == max[ i - 2 ] ) {
start[ i ] = start[ i - 2 ];
} else {
start[ i ] = start[ i - 2 ] == 0 ? 2 : start[ i - 2 ];
}
}
}
if( ( last + 1 ) % nums.length == start[ nums.length ] ) {
int[] tail = new int[nums.length-1];
System.arraycopy( nums, 1, tail, 0, nums.length-1 );
int preMax = rob2( tail );
max[ nums.length ] = Math.max( ( max[ nums.length ] - max[ 1 ] ), max[ nums.length - 1 ] );
max[ nums.length ] = Math.max( max[ nums.length ], preMax );
}
return max[ nums.length ];
} public int rob2( int[] nums ) { int[] max = new int[ nums.length + 1 ];
max[ 0 ] = 0;
if( nums == null || nums.length == 0 )
return 0;
max[ 1 ] = nums[ 0 ];
for( int i = 2; i <= nums.length; i++ ) {
max[ i ] = Math.max( max[ i - 1 ], nums[ i - 1 ] + max[ i - 2 ] );
}
return max[ nums.length ];
} public static void main( String[] args ) {
Solution s = new Solution();
int[] nums = new int[] { 2, 2, 4, 3, 2, 5 };
System.out.println( s.rob( nums ) );
} }

[LeetCode]House Robber II (二次dp)的更多相关文章

  1. [LeetCode] House Robber II 打家劫舍之二

    Note: This is an extension of House Robber. After robbing those houses on that street, the thief has ...

  2. Leetcode House Robber II

    本题和House Robber差不多,分成两种情况来解决.第一家是不是偷了,如果偷了,那么最后一家肯定不能偷. class Solution(object): def rob(self, nums): ...

  3. [LintCode] House Robber II 打家劫舍之二

    After robbing those houses on that street, the thief has found himself a new place for his thievery ...

  4. [LeetCode] Arithmetic Slices II - Subsequence 算数切片之二 - 子序列

    A sequence of numbers is called arithmetic if it consists of at least three elements and if the diff ...

  5. [LeetCode] Paint House II 粉刷房子之二

    There are a row of n houses, each house can be painted with one of the k colors. The cost of paintin ...

  6. [LeetCode] Palindrome Partitioning II 拆分回文串之二

    Given a string s, partition s such that every substring of the partition is a palindrome. Return the ...

  7. LeetCode之“动态规划”:House Robber && House Robber II

    House Robber题目链接 House Robber II题目链接 1. House Robber 题目要求: You are a professional robber planning to ...

  8. leetcode 198. House Robber 、 213. House Robber II 、337. House Robber III 、256. Paint House(lintcode 515) 、265. Paint House II(lintcode 516) 、276. Paint Fence(lintcode 514)

    House Robber:不能相邻,求能获得的最大值 House Robber II:不能相邻且第一个和最后一个不能同时取,求能获得的最大值 House Robber III:二叉树下的不能相邻,求能 ...

  9. 【刷题-LeetCode】213. House Robber II

    House Robber II You are a professional robber planning to rob houses along a street. Each house has ...

随机推荐

  1. js原生设计模式——13桥接模式(相同业务逻辑抽象化处理的职责链模式)

    桥接模式之多元化类之间的实例化调用实例 <!DOCTYPE html><html lang="en"><head>    <meta ch ...

  2. Nodejs.sublime-build 在sublime3中的配置

    { "cmd": ["node", "$file"], "file_regex": "^[ ]*File \& ...

  3. spring mvc在Controller中获取ApplicationContext

    spring mvc在Controller中获取ApplicationContext web.xml中进行正常的beans.xml和spring-mvc.xml的配置: 需要在beans.xml中进行 ...

  4. SLS评测报告

    什么是SLS?  简单日志服务(Simple Log Service,简称SLS)是针对日志收集.存储.查询和分析的服务.用户只需简单地配置日志产生的位置和格式等信息,就能实时查询海量日志,并可通过S ...

  5. eclipse xml自动提示

    找到所需要的dtd文件: window->preferences->xml->xml catalog : public id 输入对应的字符串:

  6. C++编程练习(2)----“实现简单的线性表的链式存储结构“

    单链表采用链式存储结构,用一组任意的存储单元存放线性表的元素. 对于查找操作,单链表的时间复杂度为O(n). 对于插入和删除操作,单链表在确定位置后,插入和删除时间仅为O(1). 单链表不需要分配存储 ...

  7. 笑谈ArcToolbox (3) ArcToolbox的一亩三分地

    笑谈ArcToolbox (3) ArcToolbox的一亩三分地 by 李远祥 每个人都会有一些鲜为人知的小秘密,都有着不允许别人染指的一亩三分地.软件是人编写的,当然也会给它留有自己所拥有的一亩三 ...

  8. 写给Java开发者的Node.JS简介

    前言 今天上推特看见这篇文章,点进去发现是新货. 正好最近想入Node的坑,又有一些Java基础,所以希望翻译出来给大家,同时也让自己加深理解. 才疏学浅,如有不妥之处请指正. 原文链接:Node f ...

  9. HTTP严格安全传输(HTTP Strict Transport Security, HSTS)chromuim实现源码分析(一)

    // HTTP strict transport security (HSTS) is defined in// http://tools.ietf.org/html/ietf-websec-stri ...

  10. Livy原理详解

    Livy的概述(引自社区) Livy(当前是alpha版本)是一个提供rest接口和spark集群交互的服务.它可以提交spark job或者spark一段代码,同步或者异步的返回结果:也提供spar ...