-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathlec10_12.cpp
More file actions
39 lines (29 loc) · 1018 Bytes
/
Copy pathlec10_12.cpp
File metadata and controls
39 lines (29 loc) · 1018 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
32
33
34
35
36
37
38
39
class Solution {
public:
int maxValue(vector<int>& nums) {
int n = nums.size();
int prev = nums[0];
int prev2 = 0 ;
for(int i = 1 ; i<n;i++)
{
int take = nums[i];
if(i>1) take+=prev2;
int notTake = 0 +prev;
int curri = max(take , notTake);
prev2 = prev;
prev = curri;
}
return prev;
}
int rob(vector<int>& nums) {
vector<int> temp1 , temp2;
int n = nums.size();
if(n == 1) return nums[0];
for(int i = 0 ; i<n;i++)
{
if(i !=0) temp1.push_back(nums[i]);
if(i!=n-1) temp2.push_back(nums[i]);
}
return max(maxValue(temp1),maxValue(temp2));
}
};