forked from kamyu104/LintCode
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathhouse-robber.cpp
More file actions
31 lines (27 loc) · 772 Bytes
/
house-robber.cpp
File metadata and controls
31 lines (27 loc) · 772 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
// Time: O(n)
// Space: O(1)
class Solution {
public:
/**
* @param A: An array of non-negative integers.
* return: The maximum amount of money you can rob tonight
*/
long long houseRobber(vector<int> nums) {
if (nums.size() == 0) {
return 0;
}
if (nums.size() == 1) {
return nums[0];
}
return robRange(nums, 0, nums.size());
}
long long robRange(const vector<int>& nums, const int start, const int end) {
long long num_i = nums[start], num_i_1 = 0, num_i_2 = 0;
for (int i = start + 1; i < end; ++i) {
num_i_2 = num_i_1;
num_i_1 = num_i;
num_i = max(nums[i] + num_i_2, num_i_1);
}
return num_i;
}
};