https://leetcode.com/problems/house-robber/
class Solution {
public:
int rob(vector<int>& nums) {
if (nums.size()==0)
return 0;
if (nums.size()==1)
return nums[0];
if (nums.size()==2)
{
if (nums[0]<nums[1])
return nums[1];
else
return nums[0];
}
int a=nums[0], b=nums[1], max=nums[0],temp=0;
for (int i=2; i< nums.size(); i++)
{
if ((a+nums[i]) > max)
max=a+nums[i];
temp=b;
b=a+nums[i];
if (a<temp)
a=temp;
}
if (a>b)
return a;
else
return b;
}
};