-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy path9_Airbnb_NonAdjacent_Sum.py
executable file
·54 lines (40 loc) · 1.35 KB
/
9_Airbnb_NonAdjacent_Sum.py
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
47
48
49
50
51
52
53
54
'''
This problem was asked by Airbnb.
Given a list of integers, write a function that returns the largest sum of non-adjacent numbers. Numbers can be 0 or negative.
For example, [2, 4, 6, 2, 5] should return 13, since we pick 2, 6, and 5. [5, 1, 1, 5] should return 10, since we pick 5 and 5.
Follow-up: Can you do this in O(N) time and constant space?
'''
memo_dict = {}
def max_sum(arr):
if tuple(arr) in memo_dict:
return memo_dict[tuple(arr)]
if len(arr)==0:
return 0
if len(arr)==1:
memo_dict[tuple(arr)] = arr[0]
return arr[0]
if len(arr) == 2:
memo_dict[tuple(arr)] = max(arr[0], arr[1])
return max(arr[0], arr[1])
memo_dict[tuple(arr)] = max(arr[0],max_sum(arr[2:])+arr[0], max_sum(arr[1:]))
return memo_dict[tuple(arr)]
def max_sum_const_space(arr):
# prevOne -> max from one step back
# prevTwo -> max from two steps back
# res -> current max
prevOne, prevTwo, res = 0, 0, 0
for i in range(arr.__len__()):
if i == 0:
res = arr[0]
elif i ==1:
res = max(arr[0], arr[1])
else:
res = max(arr[i], prevOne, prevTwo + arr[i])
prevTwo= prevOne
prevOne = res
return res
if __name__ == '__main__':
arr = [2, 4, 6, 2, 5]
# arr = [10,-1,-1,-1,]
res = max_sum_const_space(arr)
print(res)