-
Notifications
You must be signed in to change notification settings - Fork 0
/
009.py
35 lines (25 loc) · 881 Bytes
/
009.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
"""
Problem:
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, 8] should return 12, since we pick 4 and 8. [5, 1, 1, 5] should
return 10, since we pick 5 and 5.
"""
from typing import List
def max_nonadjacent_sum(arr: List[int]) -> int:
including = 0
excluding = 0
for elem in arr:
# updating maximum sum including and excluding the current element
including, excluding = max(excluding + elem, elem), max(excluding, including)
return max(including, excluding)
if __name__ == "__main__":
print(max_nonadjacent_sum([2, 4, 6, 8]))
print(max_nonadjacent_sum([5, 1, 1, 5]))
print(max_nonadjacent_sum([-5, 1, 1, -5]))
print(max_nonadjacent_sum([5, 5, 10, 100, 10, 5]))
"""
SPECS:
TIME COMPLEXITY: O(n)
SPACE COMPLEXITY: O(1)
"""