Skip to content

Instantly share code, notes, and snippets.

@qiaoxu123
Created November 29, 2018 01:03
Show Gist options
  • Save qiaoxu123/1d0e54ac50a122ff0e304d8ad6d9e7ac to your computer and use it in GitHub Desktop.
Save qiaoxu123/1d0e54ac50a122ff0e304d8ad6d9e7ac to your computer and use it in GitHub Desktop.
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
//比较巧妙的思路
class Solution {
public:
int rob(vector<int>& nums) {
int a = 0,b = 0;
for(int i = 0;i < nums.size();i++){
if(i % 2 == 0)
a = (a + nums[i]) > b ? (a + nums[i]) : b;
else
b = (a) > (b + nums[i]) ? a : (b + nums[i]);
}
return a > b ? a : b;
}
};
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment