House Robber

原创 2016年08月28日 14:44:23

题目:

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[i]=dp[i-1]+nums[i]  if  dp[i-1]==dp[i-2]

dp[i]=max(dp[i-2]+nums[i], dp[i-1])

根据这个动态规划等式,dp[i]为前i个house能偷到的最大数。

代码:

class Solution {
public:
    int rob(vector<int>& nums) {
        if(nums.size()==0)
        {
            return 0;
        }
        vector<int> dp(nums.size(),0);
        
        int flag;
        flag=1;
        dp[0]=nums[0];
        if(nums.size()>1)
        {
            if(nums[0]>nums[1])
            {
                dp[1]=nums[0];
                flag=0;
            }
            else
            {
                dp[1]=nums[1];
                flag=1;
            }
             for(int i=2; i<nums.size(); i++)
           {
            if(flag==0)
            {
                dp[i]=dp[i-1]+nums[i];
                flag=1;
            }
            else
            {
                dp[i]=(dp[i-2]+nums[i]);
                flag=1;
                if(dp[i-1]>dp[i])
                {
                    dp[i]=dp[i-1];
                    flag=0;
                }
            }
          }
        }
        return dp[nums.size()-1];
       
    }
};




版权声明:本文为博主原创文章,未经博主允许不得转载。

相关文章推荐

337. House Robber III

The thief has found himself a new place for his thievery again. There is only one entrance to this a...

House Robber III

c++/** * Definition for a binary tree node. * struct TreeNode { * int val; * TreeNode *l...

<LeetCode OJ> (198 / 213) House Robber(I / II)

House Robber My Submissions Question Total Accepted: 45702 Total Submissions: 142460 Difficulty:...

LeetCode-难题集之House_Robber系列

记——House_Robber,这个系列的题的思路也很简单,不过大神的代码就是给力,因此记下留着以后学习参考。 House_Robber class Solution { public: ...

LeetCode 337. House Robber III

大概题意:给定一棵二叉树,每个节点都有一个值,要求从其中选取若干个节点,是的值的和最大,约束条件为任意两个选取的节点之间不能相连。           解题的思路也很清晰,我们可以用动态规划的做法,...

leetcode 337:House Robber III

leetcode 337:House Robber III。leetcode新题,递归的动态规划

leetcode 198. House Robber

题目内容You are a professional robber planning to rob houses along a street. Each house has a certain am...

198. House Robber

You are a professional robber planning to rob houses along a street. Each house has a certain amount...

DP (9) -- Maximal Square, Maximal Rectangle,House Robber II

Maximal Square, Maximal Rectangle, House Robber II

LeetCode 198 House Robber

假设是一个小偷(这比喻,,,),在这条街上有一排房子,每一个房子都有一个确定的价值,相邻的房子不能连续偷窃,问,偷了这条街后,最大偷窃的价值总和是多少? 利用DP思想,对于从第4个房子开始,有两种选择...
内容举报
返回顶部
收藏助手
不良信息举报
您举报文章:深度学习:神经网络中的前向传播和反向传播算法推导
举报原因:
原因补充:

(最多只允许输入30个字)