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

解题思路:

房间一共有N个,先判断到目前为止,前i个房间能获得最多的金钱。

典型的动态规划。

其中转移方程如下:

maxV[i] = max( maxV[i - 2] + a[i],maxV[i-1]);

其中数组a[i]为第i个房间隐藏的金钱。maxV[i]表示前i个房间能获得的最多的钱。

代码如下:

class Solution {
public:
    int rob(vector<int>& nums) 
    {
        //处理特殊情况
    	if (nums.empty())
    		return 0;
    	if (nums.size() == 1)
    		return nums[0];
    	if (nums.size() == 2)
    		return nums[0] > nums[1] ? nums[0] : nums[1];
    	//处理正常情况
    	int * maxV = new int[nums.size()];
    
    	maxV[0] = nums[0];
    	maxV[1] = nums[0] > nums[1] ? nums[0] : nums[1];
    
    	for (int i = 2 ; i < nums.size() ; ++i)
    	{
    		maxV[i] = max(maxV[i - 2] + nums[i], maxV[i - 1]);
    	}
    
    	int result = maxV[nums.size() - 1];
    	delete maxV;
    	maxV = NULL;
    	return result;
    }
};

2016-08-31 21:49:51

时间: 2024-10-14 19:54:07

leetCode 198. House Robber | 动态规划的相关文章

[LeetCode] 198. 打家劫舍II ☆☆☆(动态规划)

描述 你是一个专业的小偷,计划偷窃沿街的房屋,每间房内都藏有一定的现金.这个地方所有的房屋都围成一圈,这意味着第一个房屋和最后一个房屋是紧挨着的.同时,相邻的房屋装有相互连通的防盗系统,如果两间相邻的房屋在同一晚上被小偷闯入,系统会自动报警. 给定一个代表每个房屋存放金额的非负整数数组,计算你在不触动警报装置的情况下,能够偷窃到的最高金额. 示例 1: 输入: [2,3,2]输出: 3解释: 你不能先偷窃 1 号房屋(金额 = 2),然后偷窃 3 号房屋(金额 = 2), 因为他们是相邻的.示例

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

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