# 198. House Robber (Easy)

## 1.問題&#x20;

* 你是位專業的強盜, 計畫搶劫一條街上的房子. 每間房子都存有各自的金額, 唯一的限制是相鄰的房子的保全系統是相連的, 而且若相鄰的房子遭破壞時將會自通知警察
* 給予一個代表每間房子內金額的list, **找出不被報警下可搶得的最大金額**

![](/files/-LHSKNmdHryZVwooC0DL)

## 2.想法&#x20;

* Dynamic programming
  * 迴圈list中的所有店家:
    * 每回合要選擇可以賺最多錢的選項
      * 1.到前前家為止所儲存錢 + 偷這一間的錢
      * 2.到前家為止所儲存錢 + 不偷這一間的錢

## 3.程式碼&#x20;

```
class Solution {
public:
    int rob(vector<int>& nums) {
        if (nums.size() <= 1)
        {
            return nums.size() == 0 ? 0 : nums[0];
        }
        int lastTime = 0, secondLastTime = 0;
        for (int l = 0; l < nums.size(); l++)
        {
            int recordLastTime = lastTime, recordSecondLastTime = secondLastTime;
            secondLastTime = recordLastTime + nums[l];
            lastTime = max(recordLastTime, recordSecondLastTime);
        }
        return max(lastTime, secondLastTime);
    }
};
```

## 4.Performance

![](/files/-LHSY2A0VJzRdOZjQmqD)


---

# Agent Instructions: Querying This Documentation

If you need additional information that is not directly available in this page, you can query the documentation dynamically by asking a question.

Perform an HTTP GET request on the current page URL with the `ask` query parameter:

```
GET https://jenhsuan.gitbook.io/algorithm/leetcode/198.-house-robber-easy.md?ask=<question>
```

The question should be specific, self-contained, and written in natural language.
The response will contain a direct answer to the question and relevant excerpts and sources from the documentation.

Use this mechanism when the answer is not explicitly present in the current page, you need clarification or additional context, or you want to retrieve related documentation sections.
