Java for LeetCode 213 House Robber II

时间:2021-08-26 23:12:35

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.

解题思路:

可以分解为两种情况:包含nums[0]和不包含nums[0],分别求解算出最大即可,JAVA实现如下;

    public int rob(int[] nums) {
if (nums == null || nums.length == 0)
return 0;
else if (nums.length <= 2)
return Math.max(nums[0], nums[nums.length - 1]);
else if(nums.length==3)
return Math.max(nums[0], Math.max(nums[1],nums[2]));
int res = 0;
int[] dp = new int[nums.length];
dp[0] = nums[0];
dp[2] = nums[0] + nums[2];
dp[3] = nums[0] + nums[3];
for (int i = 4; i < nums.length-1; i++)
dp[i] = nums[i] + Math.max(dp[i - 2], dp[i - 3]);
res=Math.max(dp[nums.length-2],dp[nums.length-3]);
dp[1] = nums[1];
dp[2] = nums[2];
dp[3] = nums[3] + nums[1];
for (int i = 4; i < nums.length; i++)
dp[i] = nums[i] + Math.max(dp[i - 2], dp[i - 3]);
return Math.max(res, Math.max(dp[nums.length-1],dp[nums.length-2]));
}

Java for LeetCode 213 House Robber II的更多相关文章

  1. &lbrack;LeetCode&rsqb; 213&period; House Robber II 打家劫舍 II

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

  2. &lbrack;LeetCode&rsqb; 213&period; House Robber II 打家劫舍之二

    You are a professional robber planning to rob houses along a street. Each house has a certain amount ...

  3. LeetCode 213&period; House Robber II

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

  4. &lbrack;leetcode&rsqb; &num;213 House Robber II Medium &lpar;medium&rpar;

    原题链接 比子母题House Robber多了一个条件:偷了0以后,第n-1间房子不能偷. 转换思路为求偷盗[0,n-1)之间,以及[1,n)之间的最大值. 用两个DP,分别保存偷不偷第0间房的情况. ...

  5. leetcode 198&period; House Robber 、 213&period; House Robber II 、337&period; House Robber III 、256&period; Paint House&lpar;lintcode 515&rpar; 、265&period; Paint House II&lpar;lintcode 516&rpar; 、276&period; Paint Fence&lpar;lintcode 514&rpar;

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

  6. 198&period; House Robber&comma;213&period; House Robber II

    198. House Robber Total Accepted: 45873 Total Submissions: 142855 Difficulty: Easy You are a profess ...

  7. 【LeetCode】213&period; House Robber II

    House Robber II Note: This is an extension of House Robber. After robbing those houses on that stree ...

  8. 【刷题-LeetCode】213&period; House Robber II

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

  9. Java for LeetCode 212 Word Search II

    Given a 2D board and a list of words from the dictionary, find all words in the board. Each word mus ...

随机推荐

  1. Java2OP

    Java2OP D:\Program Files (x86)\Embarcadero\Studio\18.0\bin\converters\java2op\Java2OP.exe Java2OP.ex ...

  2. Microsoft Mole原理及常见问题整理

     Moles与Moq(Rhino.Mocks)比较 作用范围 Moq与Rhino.Mocks这类的Mock是对Interface或AbstractClass做Mock, 而Moles是Mock整个 ...

  3. iOS--九宫格布局

    [self rankWithTotalColumns: andWithAppW: andWithAppH:]; //九宫格布局 - (void)rankWithTotalColumns:(int)to ...

  4. Android listview 的应用

    ListView作为Android最常用但是却最难用的控件之一,有很多神奇的用法.我之前也有写过一个例子,稍微不那么简单了一点. [Android原生item的伸缩效果]:http://www.cnb ...

  5. java中如何获得方法中的参数名

    在反射的时候我们可以通过class的getParameterNames()反射获得参数的名称,但是这个名称并不是参数的真实名称,而是类似于arg0,arg1等占位名称. 下面介绍一种方法获得参数真实名 ...

  6. linux远程ssh一键设置服务器时间

    cmd="sudo date -s \"$1\""; ssh mrdTomcat@*.*.*.* "$cmd" 是不是遇到过很多问题 ssh ...

  7. 数据分析面试题之Pandas中的groupby

      昨天晚上,笔者有幸参加了一场面试,有一个环节就是现场编程!题目如下:   示例数据如下,求每名学生(ID)对应的成绩(score)最高的那门科目(class)与ID,用Python实现: 这个题目 ...

  8. 2019 校内赛 RPG的地牢猎手&lpar;bfs&plus;优先队列&rpar;

    Problem Description Luke最近沉迷一款RPG游戏,游戏中角色可以进入地牢关卡,只要顺利走出地牢就可以获得奖励.地牢表示为n行m列的块矩阵,其中每个块只可以是障碍块.入口.出口或数 ...

  9. 正则表达式中&comma;&lbrack;&bsol;s&bsol;S&rsqb;&ast; 什么意思

    https://blog.csdn.net/haoyuedangkong_fei/article/details/53781936 例如:[a-z]表示从a到z之间的任意一个. 不是这样的吗?谁能给我 ...

  10. ovs flow 原理及实验

    OpenFlow概述 在支持OpenFlow的交换机中包含了若干个Flow table,Flow table可以用来控制数据包的处理,交换机会执行与flow相匹配的表项中所罗列的动作. OpenFlo ...