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.

使用动态规划DP

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

}

可以优化一下

public int rob(int[] num) {
    int prevMax = 0;
    int currMax = 0;
    for (int x : num) {
        int temp = currMax;
        currMax = Math.max(prevMax + x, currMax);
        prevMax = temp;
    }
    return currMax;
}
时间: 2024-08-08 04:38:08

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

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 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 a

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

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

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]