From aa3783bbecd67a145424c4a03058e6e08b38c99a Mon Sep 17 00:00:00 2001 From: Tapajyoti Bose Date: Wed, 17 Jun 2020 18:09:14 +0530 Subject: [PATCH 1/5] Added maximum non-adjacent sum --- dynamic_programming/max_non_adjacent_sum.py | 50 +++++++++++++++++++++ 1 file changed, 50 insertions(+) create mode 100644 dynamic_programming/max_non_adjacent_sum.py diff --git a/dynamic_programming/max_non_adjacent_sum.py b/dynamic_programming/max_non_adjacent_sum.py new file mode 100644 index 000000000000..75e17506f9d8 --- /dev/null +++ b/dynamic_programming/max_non_adjacent_sum.py @@ -0,0 +1,50 @@ +from typing import List + +def max_non_adj_sum(nums: List[int]) -> int: + ''' + Function to find the maximum non-adjacent sum of the elements in the input list + + Parameters + ---------- + nums : List[int] + List of integers for the maximum non-adjacent sum computation + + Returns + ------- + int + maximum non-adjacent sum + + Examples + -------- + These are written in doctest format, and should illustrate how to use the function + + >>> print(max_non_adj_sum([1, 2, 3])) + 4 + + >>> max_non_adj_sum([1, 5, 3, 7, 2, 2, 6]) + 18 + + >>> max_non_adj_sum([-1, -5, -3, -7, -2, -2, -6]) + 0 + + >>> max_non_adj_sum([499, 500, -3, -7, -2, -2, -6]) + 500 + ''' + + if (len(nums) == 0): + return 0 + + max_including = nums[0] + max_excluding = 0 + + for num in nums[1:]: + temp = max_including + max_including = max_excluding + num + max_excluding = max(temp, max_excluding) + + return max(max_excluding, max_including) + +if __name__ == "__main__": + import doctest + + doctest.testmod() \ No newline at end of file From f93d74afcdbfd05c4f021306e897df4a977be8d2 Mon Sep 17 00:00:00 2001 From: Tapajyoti Bose Date: Wed, 17 Jun 2020 18:40:09 +0530 Subject: [PATCH 2/5] Bugfix: flake8 test --- dynamic_programming/max_non_adjacent_sum.py | 6 ++++-- 1 file changed, 4 insertions(+), 2 deletions(-) diff --git a/dynamic_programming/max_non_adjacent_sum.py b/dynamic_programming/max_non_adjacent_sum.py index 75e17506f9d8..6a4b7790f6b4 100644 --- a/dynamic_programming/max_non_adjacent_sum.py +++ b/dynamic_programming/max_non_adjacent_sum.py @@ -1,5 +1,6 @@ from typing import List + def max_non_adj_sum(nums: List[int]) -> int: ''' Function to find the maximum non-adjacent sum of the elements in the input list @@ -41,10 +42,11 @@ def max_non_adj_sum(nums: List[int]) -> int: temp = max_including max_including = max_excluding + num max_excluding = max(temp, max_excluding) - + return max(max_excluding, max_including) + if __name__ == "__main__": import doctest - doctest.testmod() \ No newline at end of file + doctest.testmod() From d30bd17d39d8e41bca622f0f6cfa2f3dba0a5f93 Mon Sep 17 00:00:00 2001 From: Tapajyoti Bose Date: Wed, 17 Jun 2020 20:18:16 +0530 Subject: [PATCH 3/5] Implemented changes (broke tuple unpacking into 2 lines due to flake8 tests) --- dynamic_programming/max_non_adjacent_sum.py | 20 +++++++++++--------- 1 file changed, 11 insertions(+), 9 deletions(-) diff --git a/dynamic_programming/max_non_adjacent_sum.py b/dynamic_programming/max_non_adjacent_sum.py index 6a4b7790f6b4..fe042b853445 100644 --- a/dynamic_programming/max_non_adjacent_sum.py +++ b/dynamic_programming/max_non_adjacent_sum.py @@ -1,7 +1,9 @@ +# Video Explaination: https://www.youtube.com/watch?v=6w60Zi1NtL8&feature=emb_logo + from typing import List -def max_non_adj_sum(nums: List[int]) -> int: +def maximum_non_adjacent_sum(nums: List[int]) -> int: ''' Function to find the maximum non-adjacent sum of the elements in the input list @@ -19,29 +21,29 @@ def max_non_adj_sum(nums: List[int]) -> int: -------- These are written in doctest format, and should illustrate how to use the function - >>> print(max_non_adj_sum([1, 2, 3])) + >>> print(maximum_non_adjacent_sum([1, 2, 3])) 4 - >>> max_non_adj_sum([1, 5, 3, 7, 2, 2, 6]) + >>> maximum_non_adjacent_sum([1, 5, 3, 7, 2, 2, 6]) 18 - >>> max_non_adj_sum([-1, -5, -3, -7, -2, -2, -6]) + >>> maximum_non_adjacent_sum([-1, -5, -3, -7, -2, -2, -6]) 0 - >>> max_non_adj_sum([499, 500, -3, -7, -2, -2, -6]) + >>> maximum_non_adjacent_sum([499, 500, -3, -7, -2, -2, -6]) 500 ''' - if (len(nums) == 0): + if not nums: return 0 max_including = nums[0] max_excluding = 0 for num in nums[1:]: - temp = max_including - max_including = max_excluding + num - max_excluding = max(temp, max_excluding) + max_including, max_excluding = ( + max_excluding + num, max(max_including, max_excluding) + ) return max(max_excluding, max_including) From e6150f6d5cbafa816467be65c7951e4bc7a47071 Mon Sep 17 00:00:00 2001 From: Tapajyoti Bose Date: Wed, 17 Jun 2020 20:25:40 +0530 Subject: [PATCH 4/5] Implemented changes v2.0 --- dynamic_programming/max_non_adjacent_sum.py | 13 +------------ 1 file changed, 1 insertion(+), 12 deletions(-) diff --git a/dynamic_programming/max_non_adjacent_sum.py b/dynamic_programming/max_non_adjacent_sum.py index fe042b853445..b1ebeaf559aa 100644 --- a/dynamic_programming/max_non_adjacent_sum.py +++ b/dynamic_programming/max_non_adjacent_sum.py @@ -5,21 +5,10 @@ def maximum_non_adjacent_sum(nums: List[int]) -> int: ''' - Function to find the maximum non-adjacent sum of the elements in the input list - - Parameters - ---------- - nums : List[int] - List of integers for the maximum non-adjacent sum computation - - Returns - ------- - int - maximum non-adjacent sum + Find the maximum non-adjacent sum of the integers in the nums input list Examples -------- - These are written in doctest format, and should illustrate how to use the function >>> print(maximum_non_adjacent_sum([1, 2, 3])) 4 From 82f2bb8110292ffaa28204be4e814a0b283e32c5 Mon Sep 17 00:00:00 2001 From: Christian Clauss Date: Wed, 17 Jun 2020 17:08:24 +0200 Subject: [PATCH 5/5] Update max_non_adjacent_sum.py --- dynamic_programming/max_non_adjacent_sum.py | 10 ---------- 1 file changed, 10 deletions(-) diff --git a/dynamic_programming/max_non_adjacent_sum.py b/dynamic_programming/max_non_adjacent_sum.py index b1ebeaf559aa..1d771f21f3fb 100644 --- a/dynamic_programming/max_non_adjacent_sum.py +++ b/dynamic_programming/max_non_adjacent_sum.py @@ -7,33 +7,23 @@ def maximum_non_adjacent_sum(nums: List[int]) -> int: ''' Find the maximum non-adjacent sum of the integers in the nums input list - Examples - -------- - >>> 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)