-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy path213.cpp
More file actions
46 lines (34 loc) · 1.13 KB
/
213.cpp
File metadata and controls
46 lines (34 loc) · 1.13 KB
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
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
/*
* Written by Nitin Kumar Maharana
* nitin.maharana@gmail.com
*/
class Solution {
public:
int rob(vector<int>& nums) {
int len = nums.size();
if(len == 0)
return 0;
if(len == 1)
return nums[0];
int includeFirst, includeLast;
pair<int, int> includeornot;
int temp;
includeornot = make_pair(0,0);
for(int i = 0; i < len-1; i++)
{
temp = includeornot.second + nums[i];
includeornot.second = max(includeornot.first, includeornot.second);
includeornot.first = temp;
}
includeFirst = max(includeornot.first, includeornot.second);
includeornot = make_pair(0,0);
for(int i = 1; i < len; i++)
{
temp = includeornot.second + nums[i];
includeornot.second = max(includeornot.first, includeornot.second);
includeornot.first = temp;
}
includeLast = max(includeornot.first, includeornot.second);
return max(includeFirst, includeLast);
}
};