198. House Robber
class Solution {
public:
int rob(vector<int>& nums) {
int n = nums.size();
int a = 0, b= 0;
for(int i = 0 ; i < n; i++){
if(i%2 == 0){
a = max(b, a + nums[i]);
}
else{
b = max(a, b + nums[i]);
}
}
return max(a,b);
}
};Last updated
Was this helpful?