-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathproblem9.py
More file actions
28 lines (21 loc) · 792 Bytes
/
Copy pathproblem9.py
File metadata and controls
28 lines (21 loc) · 792 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
# 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?
def max_sum(list):
if len(list) == 0:
return 0
included = list[0]
excluded = 0
for i in list[1:]:
prev_max = max(included, excluded)
included = excluded + i
excluded = prev_max
return max(included,excluded)
def main():
test_arr1 = [5,1,1,5]
test_arr2 = [2,4,6,2,5]
print max_sum(test_arr1)
print max_sum(test_arr2)
if __name__ == '__main__':
main()