码迷,mamicode.com
首页 > 其他好文 > 详细

198. House Robber

时间:2016-08-19 00:54:20      阅读:144      评论:0      收藏:0      [点我收藏+]

标签:

1. 问题描述

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.
Tags: Dynamic Programming
Similar Problems: (M) Maximum Product Subarray (M) House Robber II (M) Paint House (E) Paint Fence (M) House Robber III

2. 解题思路

  • 动态规划

3. 代码

 1 class Solution {
 2 public:
 3     int rob(vector<int>& nums)
 4     {
 5         int vSize = nums.size();
 6         if (0 == vSize)
 7         {
 8             return 0;
 9         }
10         int Rob_last = 0;
11         int UnRob_last = 0;    
12         int Rob = 0;
13         int UnRob = 0;
14 
15         for (int i=0; i<vSize; i++)
16         {
17             Rob = nums[i] + UnRob_last;
18             UnRob = Rob_last > UnRob_last ? Rob_last : UnRob_last;
19 
20             UnRob_last = UnRob;
21             Rob_last = Rob;
22         }
23 
24         return Rob > UnRob ? Rob : UnRob;
25     }
26 };

4. 反思

198. House Robber

标签:

原文地址:http://www.cnblogs.com/whl2012/p/5785928.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!