国产探花免费观看_亚洲丰满少妇自慰呻吟_97日韩有码在线_资源在线日韩欧美_一区二区精品毛片,辰东完美世界有声小说,欢乐颂第一季,yy玄幻小说排行榜完本

首頁 > 學院 > 開發設計 > 正文

[LeetCode] House Robber II

2019-11-15 01:15:06
字體:
來源:轉載
供稿:網友
[LeetCode] House Robber II

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 arearranged 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 tonightwithout alerting the police.

這道題和之前它的基礎版最大的不同就是所有house圍成了一個圈。之前我們只需要考慮這個house的前一個是否被rob與否?,F在呢就有了特殊情況。

第一個和最后一個house不能同時被rob。

最為簡單的方法就是call兩次第一次的算法。(一個assume第一個house被rob,一個則assume最后一個house被rob。)

因為這里有nums.length-2,因此special case里面應該有nums.length==1的時候的單獨的算法。

代碼如下。~

public class Solution {    public int rob(int[] nums) {        if(nums.length==0||nums==null){            return 0;        }        return Math.max(rob1(nums,0,nums.length-2),rob1(nums,1,nums.length-1));                    }        private int rob1(int[]nums, int start,int end){         if(nums.length==0||nums==null){            return 0;         }         if(nums.length==1){             return nums[0];         }         int prerob=0;         int predontrob=0;         for(int i=start;i<end+1;i++){             int curr=predontrob+nums[i];             int dontcurr=Math.max(prerob,predontrob);             prerob=curr;             predontrob=dontcurr;         }         return Math.max(prerob,predontrob);             }}


發表評論 共有條評論
用戶名: 密碼:
驗證碼: 匿名發表
主站蜘蛛池模板: 曲沃县| 阿拉尔市| 宁津县| 含山县| 宜良县| 峨边| 松江区| 江北区| 桐乡市| 米脂县| 鄂尔多斯市| 石楼县| 司法| 尚志市| 方正县| 邯郸市| 南投县| 敦煌市| 彩票| 班戈县| 南溪县| 漳平市| 察哈| 庆城县| 庄河市| 绥阳县| 高邮市| 定日县| 葵青区| 崇义县| 新安县| 桐梓县| 高青县| 来安县| 通江县| 澎湖县| 白银市| 永川市| 盐山县| 军事| 甘泉县|