标签:
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
动态规划
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 };
标签:
原文地址:http://www.cnblogs.com/whl2012/p/5785928.html