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 = nums.size();
    if (len <= 0)
      return res;
    int dp[len];
    for (int i = 0; i < len; i++)
    {
      dp[i] = nums[i];
      res = max(res, dp[i]);
    }
    if (len > 2)
    {
      dp[2] += dp[0];
      res = max(res, dp[2]);
    }
    for (int j = 3; j < len; j++)
    {
      dp[j] += max(dp[j - 2], dp[j - 3]);
      res = max(res, dp[j]);
    }
    return res;
  }
};

后面DP思路改成:dp[i]记录在偷到第i位时,最大可偷多少钱。

可偷最多的钱要么是偷这次的,要么是不偷这一次的。

转移方程为: dp[i]=max(dp[i-2]+nums[i],dp[i-1])

Runtime: 0 ms, faster than 100.00%

class Solution
{
public:
  int rob(vector<int> &nums)
  {
    int res = 0;
    int len = nums.size();
    if (len <= 0)
      return res;
    if (len == 1)
      return nums[0];
    if (len == 2)
      return (nums[0] > nums[1] ? nums[0] : nums[1]);
    int dp[len];
    dp[0] = nums[0];
    dp[1] = max(nums[0], nums[1]);

    for (int i = 2; i < len; i++)
    {
      dp[i] = max(dp[i - 2] + nums[i], dp[i - 1]);
    }
    return dp[len - 1];
  }
};

原文地址:https://www.cnblogs.com/ruoh3kou/p/9906827.html

时间: 2024-10-09 10:49:37

leetcode 198. House Robber (Easy)的相关文章

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 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 求抢劫的最大金额 ---------- 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

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

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

LeetCode 198, 213 House Robber

198 House Robber DP 0~n-1     ans=dp[n-1] dp[i] = max(dp[i-2]+nums[i], dp[i-1])  i>=2 213 House Robber II Since we cannot rob nums[0] and nums[n-1] at the same time, we can divide this problem into two cases: not rob nums[0] not rob nums[n-1] and the