GV

GeezerVison

The World Wide Web, restored to 800 pixels of glory.

Home|About|Gaming cabinet|Archives|How it works|Starter sites

On this site

  • Sign in
  • ActionsCI/CD and automation
  • CodespacesCloud development environments
  • All Features
  • Documentation
  • Training
  • Certifications
  • Build with Copilot
  • Customize Copilot
  • Integrations
  • Trending repos
  • GitHub Sponsors
  • Developer articles
  • Enterprises

This view

  • Original page
  • New address

GeezerVison is showing github.com — Added maximum non-adjacent sum (#2130) · jusing-es/Python@23484ef

File tree

Expand file treeCollapse file tree
Open diff view settings
Filter options
  • dynamic_programming
    • max_non_adjacent_sum.py
Expand file treeCollapse file tree
Open diff view settings
Collapse file

‎dynamic_programming/max_non_adjacent_sum.py‎

Copy file name to clipboard
+33Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
# Video Explaination: https://www.youtube.com/watch?v=6w60Zi1NtL8&feature=emb_logo
2+
3+
from typing import List
4+
5+
6+
def maximum_non_adjacent_sum(nums: List[int]) -> int:
7+
'''
8+
Find the maximum non-adjacent sum of the integers in the nums input list
9+
10+
>>> print(maximum_non_adjacent_sum([1, 2, 3]))
11+
4
12+
>>> maximum_non_adjacent_sum([1, 5, 3, 7, 2, 2, 6])
13+
18
14+
>>> maximum_non_adjacent_sum([-1, -5, -3, -7, -2, -2, -6])
15+
0
16+
>>> maximum_non_adjacent_sum([499, 500, -3, -7, -2, -2, -6])
17+
500
18+
'''
19+
if not nums:
20+
return 0
21+
max_including = nums[0]
22+
max_excluding = 0
23+
for num in nums[1:]:
24+
max_including, max_excluding = (
25+
max_excluding + num, max(max_including, max_excluding)
26+
)
27+
return max(max_excluding, max_including)
28+
29+
30+
if __name__ == "__main__":
31+
import doctest
32+
33+
doctest.testmod()

0 commit comments

Comments
0 (0)
You are visitor 01402792 — GeezerVison does not store the pages you look up.
Best viewed with Netscape Navigator 4.7 or Internet Explorer 6 at 800×600. No cookies required.