[LeetCode] 198. House Robber Java

题目:

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.

题意及分析:给出一个非负的数组,从头到尾遍历,求取一个能得到最大和的序列,其中每两个元素不能相邻。使用动态规划,到当前元素i的最大收益只有两种情况:

(1)前一个元素得到最大值

(2)从前一个元素的前一个元素加上当前元素得到最大值

比较两个值,取最大值为当前点的最大收益

代码:

public class Solution {
    public int rob(int[] nums) {
        int max=Integer.MIN_VALUE;

        int length=nums.length;
        if(length==0)
        	return 0;
        if(length==1)
        	return nums[0];
        if(length==2)
        	return Math.max(nums[0], nums[1]);
        int[] res = new int[length];
        res[0]=nums[0];
        res[1]=Math.max(nums[0], nums[1]);
        max=Math.max(nums[0], nums[1]);
        for(int i=2;i<length;i++){
        	max=Math.max(res[i-2]+nums[i], res[i-1]);
        	res[i]=max;
        }
        return res[length-1];
    }
}

  

时间: 2024-08-02 07:00:01

[LeetCode] 198. House Robber Java的相关文章

leetCode 198. House Robber | 动态规划

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

Java for 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

leetcode 198. House Robber 求抢劫的最大金额 ---------- java

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 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 Java Solutions

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——198 House Robber(寻找数组不相邻组合最大值—动态规划问题)

<span style="font-family: Arial, Helvetica, sans-serif; background-color: rgb(255, 255, 255);">You are a professional robber planning to rob houses along a street. Each house has a certain amount of money stashed(藏着), </span> the onl

动态规划系列 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

leetcode 198. House Robber (Easy)

https://leetcode.com/problems/house-robber/ 题意: 一维数组,相加不相邻的数组,返回最大的结果. 思路: 一开始思路就是DP,用一维数组保存dp[i]保存如果偷第i间,此时可偷到多少.DP的方向不太好,所以效率很低. Runtime: 4 ms, faster than 17.53% class Solution { public: int rob(vector<int> &nums) { int res = 0; int len = num

leetcode 198 House Robber I

function rob(nums) { if(!nums || nums.length === 0) { return 0; } else if(nums.length < 2){ return nums[0]; } let memo = new Array(nums.length); memo[0] = nums[0]; memo[1] = Math.max(nums[0], nums[1]); for(let i = 2; i < nums.length; i++) { memo[i]