GeezerVisonThe World Wide Web, restored to 800 pixels of glory. |
| Home|About|Gaming cabinet|Archives|How it works|Starter sites |
GeezerVison is showing github.com — Python/dynamic_programming/max_non_adjacent_sum.py at master · shitkoder/Python Expand file tree/ Copy pathmax_non_adjacent_sum.pyMore file actions 34 lines (27 loc) · 886 Bytes / Copy pathmax_non_adjacent_sum.pyFile metadata and controls34 lines (27 loc) · 886 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 29 30 31 32 33 34 # Video Explanation: https://www.youtube.com/watch?v=6w60Zi1NtL8&feature=emb_logo from __future__ import annotations def maximum_non_adjacent_sum(nums: list[int]) -> int: """ Find the maximum non-adjacent sum of the integers in the nums input list >>> print(maximum_non_adjacent_sum([1, 2, 3])) 4 >>> maximum_non_adjacent_sum([1, 5, 3, 7, 2, 2, 6]) 18 >>> maximum_non_adjacent_sum([-1, -5, -3, -7, -2, -2, -6]) 0 >>> maximum_non_adjacent_sum([499, 500, -3, -7, -2, -2, -6]) 500 """ if not nums: return 0 max_including = nums[0] max_excluding = 0 for num in nums[1:]: max_including, max_excluding = ( max_excluding + num, max(max_including, max_excluding), ) return max(max_excluding, max_including) if __name__ == "__main__": import doctest doctest.testmod() |
| You are visitor 01402775 — 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. |