House Robber II——Leetcode

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.

题目大意:所有的住户围成一圈,只能隔着抢劫,问可以抢到的总额最大是多少。

解题思路:

因为第一家和最后一家是连着的,所以抢第一家就不能抢最后一家,抢最后一家就不能抢第一家,那么可以把数组分成两段,0~n-1和1~n两段,分别计算最大的,取较大的。

    public int rob(int[] nums) {
        if(nums==null||nums.length==0){
            return 0;
        }
        if(nums.length==1){
            return nums[0];
        }
        if(nums.length==2){
            return Math.max(nums[0],nums[1]);
        }
        int res = -1;
        int[] max = new int[nums.length+1];
        max[0]=nums[0];
        max[1]=Math.max(nums[0],nums[1]);
        for(int i=2;i<nums.length-1;i++){
            max[i]=Math.max(max[i-1],max[i-2]+nums[i]);
        }
        res=Math.max(res,max[nums.length-2]);
        Arrays.fill(max,0);
        max[1]=nums[1];
        max[2]=Math.max(nums[1],nums[2]);
        for(int i=3;i<nums.length;i++){
            max[i]=Math.max(max[i-1],max[i-2]+nums[i]);
        }
        res=Math.max(res,max[nums.length-1]);
        return res;
    }
时间: 2024-10-09 05:54:48

House Robber II——Leetcode的相关文章

Housse Robber II | leetcode

可以复用house robber的代码,两趟dp作为两种情况考虑,选最大值 #include <stdio.h> #define MAX 1000 #define max(a,b) ( (a)>(b)?(a):(b) ) int dp[MAX]={0}; int rob1(int* a, int n) { int i; dp[0]=0; dp[1]=a[0]; for(i=1;i<n;i++){ dp[i+1]=max(dp[i],a[i]+dp[i-1]); } return d

House Robber II -- leetcode

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. 

leetCode 213. House Robber II | Medium | Dynamic Programming

213. House Robber II 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 a

【LeetCode】213. House Robber II

House Robber II 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 arrang

LeetCode之“动态规划”:House Robber &amp;&amp; House Robber II

House Robber题目链接 House Robber II题目链接 1. House Robber 题目要求: You are a professional robber planning to rob houses along a street. Each house has a certain amount of money stashed, the only constraint stopping you from robbing each of them is that adjac

[LintCode] 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 are arranged in a circle. That means the first house is the neighbor o

Pascal&#39;s Triangle II Leetcode java

题目: Given an index k, return the kth row of the Pascal's triangle. For example, given k = 3, Return [1,3,3,1]. Note: Could you optimize your algorithm to use only O(k) extra space? 题解: 为了达到O(k)的空间复杂度要求,那么就要从右向左生成结果.相当于你提前把上一行的计算出来,当前行就可以用上一次计算出的结果计算了

Spiral Matrix II leetcode java

题目: Given an integer n, generate a square matrix filled with elements from 1 to n2 in spiral order. For example, Given n = 3, You should return the following matrix: [ [ 1, 2, 3 ], [ 8, 9, 4 ], [ 7, 6, 5 ] ] 题解:这道题跟Spiral Matrix想法也是类似的,就是依照矩阵从外圈到内圈建立

Permutations II leetcode java

题目: Given a collection of numbers that might contain duplicates, return all possible unique permutations. For example, [1,1,2] have the following unique permutations: [1,1,2], [1,2,1], and [2,1,1]. 题解: 这道题跟Permutaitons没啥大的区别,就是结果去重. 我之前也有写过去重的两个方法: 一