---
# System prepended metadata

title: Leetcode 198. House Robber __ Medium
tags: [Leetcode]

---

# Leetcode 198. House Robber __ Medium
###### tags: `Leetcode`

OS:第一次自己完成Medium題目，讚讚!

題目
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 systems connected and it will automatically contact the police if two adjacent houses were broken into on the same night.

Given an integer array nums representing the amount of money of each house, return the maximum amount of money you can rob tonight without alerting the police.

解法
這題只要看得懂題目，基本上照著做不會太難的，基本上用走樓梯一次只能走一格或兩格的那種邏輯來做就可以了，(當下這一格，只能從前一格或是前兩格走過來)，這題則是偷當下這家的錢，只能從前兩家或是前三家來

```


int rob(int* nums, int numsSize){
    int money = 0;
    int temp1 = 0;
    int temp2 = 0;
    for(int i = 0; i < numsSize; i++)
    {
        
        if(i-3>=0)
        {
            temp1 = nums[i-2] + nums[i];
            temp2 = nums[i-3] + nums[i];
            if(temp1>temp2)
                nums[i] = temp1;
            else
                nums[i] = temp2;
            
        }
        else if(i-2>=0)
        {
            temp1 = nums[i-2] + nums[i];
            nums[i] = temp1;
        }
        if(nums[i] > money)
            money = nums[i];
    }
    return money;
}
```