From e3b3b126bdc87a07fa2d2e8164b121f9d39a547b Mon Sep 17 00:00:00 2001 From: Cho Yin Yong Date: Sat, 24 Oct 2020 01:10:08 -0400 Subject: [PATCH 1/4] A divide and conquer method in finding the maximum difference pair --- divide_and_conquer/max_difference_pair.py | 44 +++++++++++++++++++++++ 1 file changed, 44 insertions(+) create mode 100644 divide_and_conquer/max_difference_pair.py diff --git a/divide_and_conquer/max_difference_pair.py b/divide_and_conquer/max_difference_pair.py new file mode 100644 index 000000000000..72cae71b53ae --- /dev/null +++ b/divide_and_conquer/max_difference_pair.py @@ -0,0 +1,44 @@ +from typing import List + + +def max_difference(a: List[int]) -> (int, int): + """ + We are given an array A[1..n] of integers, n >= 1. We want to + find a pair of indices (i, j) such that + 1 <= i <= j <= n and A[j] - A[i] is as large as possible. + + Explanation: + https://www.geeksforgeeks.org/maximum-difference-between-two-elements/ + + >>> max_difference([5, 11, 2, 1, 7, 9, 0, 7]) + (1, 9) + """ + # base case + if len(a) == 1: + return a[0], a[0] + else: + # split A into half. + first = a[:len(a)//2] + second = a[len(a)//2:] + + # 2 sub problems, 1/2 of original size. + small1, big1 = max_difference(first) + small2, big2 = max_difference(second) + + # get min of first and max of second + # linear time + min_first = min(first) + max_second = max(second) + + # 3 cases, either (small1, big1), + # (min_first, max_second), (small2, big2) + # constant comparisons + if ( + big2 - small2 > max_second - min_first + and big2 - small2 > big1 - small1 + ): + return small2, big2 + elif big1 - small1 > max_second - min_first: + return small1, big1 + else: + return min_first, max_second From 39b346e237d5f93e02e808cd778810641061cebb Mon Sep 17 00:00:00 2001 From: Cho Yin Yong Date: Sat, 24 Oct 2020 01:16:01 -0400 Subject: [PATCH 2/4] fix formatting issues --- divide_and_conquer/max_difference_pair.py | 13 +++++-------- 1 file changed, 5 insertions(+), 8 deletions(-) diff --git a/divide_and_conquer/max_difference_pair.py b/divide_and_conquer/max_difference_pair.py index 72cae71b53ae..9724218fd593 100644 --- a/divide_and_conquer/max_difference_pair.py +++ b/divide_and_conquer/max_difference_pair.py @@ -6,10 +6,10 @@ def max_difference(a: List[int]) -> (int, int): We are given an array A[1..n] of integers, n >= 1. We want to find a pair of indices (i, j) such that 1 <= i <= j <= n and A[j] - A[i] is as large as possible. - + Explanation: https://www.geeksforgeeks.org/maximum-difference-between-two-elements/ - + >>> max_difference([5, 11, 2, 1, 7, 9, 0, 7]) (1, 9) """ @@ -18,8 +18,8 @@ def max_difference(a: List[int]) -> (int, int): return a[0], a[0] else: # split A into half. - first = a[:len(a)//2] - second = a[len(a)//2:] + first = a[: len(a)//2] + second = a[len(a)//2 :] # 2 sub problems, 1/2 of original size. small1, big1 = max_difference(first) @@ -33,10 +33,7 @@ def max_difference(a: List[int]) -> (int, int): # 3 cases, either (small1, big1), # (min_first, max_second), (small2, big2) # constant comparisons - if ( - big2 - small2 > max_second - min_first - and big2 - small2 > big1 - small1 - ): + if big2 - small2 > max_second - min_first and big2 - small2 > big1 - small1: return small2, big2 elif big1 - small1 > max_second - min_first: return small1, big1 From 22ab8844ed9382579dd049e0018e956911c88b35 Mon Sep 17 00:00:00 2001 From: Cho Yin Yong Date: Sat, 24 Oct 2020 01:26:33 -0400 Subject: [PATCH 3/4] fix formatting issues --- divide_and_conquer/max_difference_pair.py | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/divide_and_conquer/max_difference_pair.py b/divide_and_conquer/max_difference_pair.py index 9724218fd593..fea797543ee8 100644 --- a/divide_and_conquer/max_difference_pair.py +++ b/divide_and_conquer/max_difference_pair.py @@ -18,8 +18,8 @@ def max_difference(a: List[int]) -> (int, int): return a[0], a[0] else: # split A into half. - first = a[: len(a)//2] - second = a[len(a)//2 :] + first = a[: len(a) // 2] + second = a[len(a) // 2 :] # 2 sub problems, 1/2 of original size. small1, big1 = max_difference(first) From 60e37a28bb38263e2f680792f0fc1d627f5eff1e Mon Sep 17 00:00:00 2001 From: Cho Yin Yong Date: Sat, 24 Oct 2020 14:35:52 -0400 Subject: [PATCH 4/4] add doctest runner --- divide_and_conquer/max_difference_pair.py | 6 ++++++ 1 file changed, 6 insertions(+) diff --git a/divide_and_conquer/max_difference_pair.py b/divide_and_conquer/max_difference_pair.py index fea797543ee8..b976aca43137 100644 --- a/divide_and_conquer/max_difference_pair.py +++ b/divide_and_conquer/max_difference_pair.py @@ -39,3 +39,9 @@ def max_difference(a: List[int]) -> (int, int): return small1, big1 else: return min_first, max_second + + +if __name__ == "__main__": + import doctest + + doctest.testmod()