forked from sachuverma/DataStructures-Algorithms
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathStickler Thief.cpp
More file actions
43 lines (36 loc) · 1.23 KB
/
Stickler Thief.cpp
File metadata and controls
43 lines (36 loc) · 1.23 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
/*
Stickler Thief
==============
Stickler the thief wants to loot money from a society having n houses in a single line. He is a weird person and follows a certain rule when looting the houses. According to the rule, he will never loot two consecutive houses. At the same time, he wants to maximize the amount he loots. The thief knows which house has what amount of money but is unable to come up with an optimal looting strategy. He asks for your help to find the maximum money he can get if he strictly follows the rule. Each house has a[i] amount of money present in it.
Example 1:
Input:
n = 6
a[] = {5,5,10,100,10,5}
Output: 110
Explanation: 5+100+5=110
Example 2:
Input:
n = 3
a[] = {1,2,3}
Output: 4
Explanation: 1+3=4
Your Task:
Complete the function FindMaxSum() which takes an array arr[] and n as input which returns the maximum money he can get following the rules
Expected Time Complexity: O(N).
Expected Space Complexity: O(N).
Constraints:
1 <= n <= 104
1 <= a[i] <= 104
*/
ll FindMaxSum(ll arr[], ll n)
{
ll including = arr[0], excluding = 0;
ll new_exc;
for (auto i = 1; i < n; ++i)
{
new_exc = max(including, excluding);
including = arr[i] + excluding;
excluding = new_exc;
}
return max(including, excluding);
}