# House Robber

## Desicription

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.

Example 1:

```Input: [1,2,3,1]
Output: 4
Explanation: Rob house 1 (money = 1) and then rob house 3 (money = 3).
Total amount you can rob = 1 + 3 = 4.```

Example 2:

```Input: [2,7,9,3,1]
Output: 12
Explanation: Rob house 1 (money = 2), rob house 3 (money = 9) and rob house 5 (money = 1).
Total amount you can rob = 2 + 9 + 1 = 12.```

## Solution

```class Solution {
public:
int rob(vector<int>& nums) {
int a = 0;
int b = 0;
for(int i = 0; i < nums.size(); i++) {
if(i & 1)
a = max(a + nums[i], b);
else
b = max(b + nums[i], a);
}
return max(a, b);
}
};```

0 条评论

• ### Baozi Training Leetcode solution 198. House Robber

You are a professional robber planning to rob houses along a street. Each house ...

• ### 198. 打家劫舍

你是一个专业的小偷，计划偷窃沿街的房屋。每间房内都藏有一定的现金，影响你偷窃的唯一制约因素就是相邻的房屋装有相互连通的防盗系统，如果两间相邻的房屋在同一晚上被小...

• ### 【Leetcode 198】关关的刷题日记69 – Leetcode 198 House Robber

关关的刷题日记69 – Leetcode 198 House Robber 题目 You are a professional robber planning ...

• ### Leetcode: House Robber

题目： You are a professional robber planning to rob houses along a street. Each ...

• ### Leetcode 198 House Robber

You are a professional robber planning to rob houses along a street. Each house...

• ### leetcode 198 House Robber

You are a professional robber planning to rob houses along a street. Each house ...

• ### 动态规划算法练习（5）--medium

中文链接：https://leetcode-cn.com/problems/predict-the-winner/ 英文链接：https://leetcode...

• ### Leetcode 213 House Robber II

Note: This is an extension of House Robber. After robbing those houses on that...

• ### Leetcode 213. House Robber II

版权声明：博客文章都是作者辛苦整理的，转载请注明出处，谢谢！ https://blog.csdn....

• ### LeetCode 198. House Robber(DP)

题意：给你一个数组表示一个街道，每个数字表示一个房子里有多少钱，你可以打劫获得这些钱，但是不能同时打劫相邻的房子，问你最多可以得到多少钱，

• ### LeetCode 0337 - House Robber III

The thief has found himself a new place for his thievery again. There is only on...

• ### LeetCode 0213 - House Robber II

You are a professional robber planning to rob houses along a street. Each house ...

• ### Golang Leetcode 213. House Robber II.go

版权声明：原创勿转 https://blog.csdn.net/anakinsun/article/details/89043336

• ### Golang Leetcode 337. House Robber III.go

版权声明：原创勿转 https://blog.csdn.net/anakinsun/arti...

• ### 动态规划|相邻约束下的最优解(House Robber II )

01 House Robber II This time, all houses at this place are arranged in a circl...

• ### 经典动态规划：打家劫舍系列问题

有好几位读者私下问我 LeetCode 「打家劫舍」系列问题（英文版叫 House Robber）怎么做，我发现这一系列题目的点赞非常之高，是比较有代表性和技巧...

• ### House Robber

ou are a professional robber planning to rob houses along a street. Each house h...

### Reck Zhang

TencentGame Developer

Tencent · Game Developer (已认证)