# 算法练习（-）

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

<code>NSArray *a = @[@(20),@(1),@(2),@(3)];</code>

<code>iOS</code>代码：

``````NSUInteger max(NSUInteger a, NSUInteger b) {
return  a>b?a:b;
}

NSUInteger maxNumber(NSArray *nums) {
NSUInteger result = 0;

///观察最大的值变化
NSMutableArray *max = [NSMutableArray new];
for (int i = 0; i < nums.count; ++i) {
NSUInteger n1 = 0;
NSUInteger n2 = 0;
NSUInteger n;
if (i - 2 >= 0) {
n1 = [max[i - 2] integerValue];
}
if (i - 3 >= 0) {
n2 = [max[i - 3] integerValue];
}

if (n1 > n2) {
n = [nums[i] integerValue] + n1;
} else {
n = [nums[i] integerValue] + n2;
}
if (n > result) {
result = n;
}
}

[max enumerateObjectsUsingBlock:^(id  _Nonnull obj, NSUInteger idx, BOOL * _Nonnull stop) {
NSLog(@"🐒🐒🐒🐒 =   %@",obj);
}];

return result;
}

``````

``````NSUInteger rob(NSArray *nums) {
NSUInteger a = 0, b = 0;
for (int i = 0; i < nums.count; ++i) {
if(i % 2 == 0)a = max(a+[nums[i] integerValue],b);
else b = max(b+[nums[i] integerValue],a);

NSLog(@"%@ %@", @(a),@(b));
}
return max(a,b);
}

``````