728x90
def max_sum_non_adjacent(arr, index=0):
if index >= len(arr):
return 0
# Include the current element and skip the next
include = arr[index] + max_sum_non_adjacent(arr, index + 2)
# Exclude the current element and move to the next
exclude = max_sum_non_adjacent(arr, index + 1)
# Return the maximum of including or excluding the current element
return max(include, exclude)
# Example arrays
arr1 = [12, 162, 37, 361]
arr2 = [16, 26, 37, 163, 37]
arr3 = [26, 27, 1738, 163, 1737, 1637, 1637, 16372, 16374, 2757, 2637, 16372]
print("Maximum sum for arr1:", max_sum_non_adjacent(arr1))
print("Maximum sum for arr2:", max_sum_non_adjacent(arr2))
print("Maximum sum for arr3:", max_sum_non_adjacent(arr3))
728x90
'호그와트' 카테고리의 다른 글
crazy monster made by tryhackme GOD :: hero (0) | 2024.05.14 |
---|---|
the dancing rabbit (0) | 2024.05.13 |
Docker 설치 진짜 개 쉽게 하는 법 (Windows WSL 2) (1) | 2024.05.13 |
넘어간다제~ (0) | 2024.05.06 |
tryhackme athena fantasia :: tryhackme GOD의 풀이 (2) | 2024.05.05 |