leetcode_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 adjacent houses have security system connected and it
will automatically contact the police if two adjacent houses were broken into on the same night.

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.

思路:

动态规划问题,只需要保证当前获得的就是最大的值就可以了

代码:

public int rob(int[] nums) {
        if(nums==null)
            return 0;
        int len=nums.length;
        int maxProfit[]=null;
        if(len==0)
            return 0;
        else if(len==1)
            return nums[0];
        else if(len==2)
            return Math.max(nums[0],nums[1]);
        else
        {
            maxProfit=new int[len];
            maxProfit[0]=nums[0];
            maxProfit[1]=Math.max(nums[0],nums[1]);
            for(int i=2;i<len;i++)
                maxProfit[i]=Math.max(maxProfit[i-1],maxProfit[i-2]+nums[i]);
        }
        return maxProfit[len-1];
    }
时间: 2024-10-29 19:11:29

leetcode_House Robber的相关文章

leetcode 213. House Robber II 抢劫房子II---------- java

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] 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 adjacent houses have security system connected and it will autom

【leetcode】House Robber (middle)

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 adjacent houses have security system connected and it will autom

[leetcode] House Robber python 解决方案

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 adjacent houses have security system connected and it will autom

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 adjacent houses have security system connected and it will a

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 arranged in a circ

LeetCode 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 arranged in a circle. 

LeetCode 198. 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 adjacent houses have security system connected and it will autom

198,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 adjacent houses have security system connected and it will