Leetcode 198. House Robber

https://leetcode.com/problems/house-robber/

题意:抢劫,返回能抢到的最大数值,但是如果连续抢两个的话就会报警

思路:不会做伤心。。。

动态规划问题,遍历数组,对每一个元素考虑两种情况,抢它和不抢它

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
class Solution {
public:
int rob(vector<int>& nums) {
int preRob = 0, preNotRob = 0;
int rob = 0, notRob = 0;
for(int i = 0; i < nums.size(); i++){
preRob = rob;
preNotRob = notRob;
rob = preNotRob + nums[i];
notRob = max(preRob, preNotRob);
}

return max(rob, notRob);
}
};
0%