-
Notifications
You must be signed in to change notification settings - Fork 7
Expand file tree
/
Copy pathMax Subset Sum No Adjacent.py
More file actions
49 lines (36 loc) · 1.08 KB
/
Max Subset Sum No Adjacent.py
File metadata and controls
49 lines (36 loc) · 1.08 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
44
45
46
47
48
49
"""
Max Subset Sum No Adjacent
Write a function that takes in array of positive integers and returns the maximum
sum of non-adjacent elements in the array.
If the input array is empty ,the function should return 0.
Sample Input
array = [75, 105, 120, 75, 90, 135]
Sample Output
330 // 75 + 120 + 135
"""
# SOLUTION 1
# O(n) time | O(n) space
def maxSubsetSumNoAdjacent(array):
if not len(array):
return 0
elif len(array) == 1:
return array[0]
maxSums = array[:]
maxSums[1] = max(array[0], array[1])
for i in range(2, len(array)):
maxSums[i] = max(maxSums[i-1], maxSums[i-2] + array[i])
return maxSums[-1]
# SOLUTION 2
# O(n) time | O(1) space
def maxSubsetSumNoAdjacent(array):
if not len(array):
return 0
elif len(array) == 1:
return array[0]
second = array[0]
first = max(array[0], array[1])
for i in range(2, len(array)):
current = max(first, second + array[i])
second = first
first = current
return first