-
-
Notifications
You must be signed in to change notification settings - Fork 48.7k
Add new algorithm for max-min problem #11694
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Open
FuturistForever
wants to merge
2
commits into
TheAlgorithms:master
Choose a base branch
from
FuturistForever:branch-2
base: master
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from 1 commit
Commits
Show all changes
2 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,84 @@ | ||
""" | ||
The Max-Min Problem is finding the maximum and minimum value in an array. | ||
|
||
This is a divide and conquer approach to solve the Max-Min Problem. | ||
|
||
For more information: | ||
https://www.tutorialspoint.com/data_structures_algorithms/max_min_problem.htm | ||
""" | ||
|
||
|
||
class Pair: | ||
""" | ||
Structure to store both maximum and minimum elements | ||
""" | ||
|
||
def __init__(self): | ||
self.max = 0 | ||
self.min = 0 | ||
|
||
|
||
def max_min_divide_conquer(arr: list, low: int, high: int) -> "Pair": | ||
""" | ||
Returns the maximum and minimum elements in the array. | ||
|
||
Args: | ||
arr: A list of integers | ||
low: An integer representing the start index of the array | ||
high: An integer representing the end index of the array | ||
|
||
Returns: | ||
A Pair object containing the maximum and minimum elements in the array | ||
|
||
>>> arr = [1, 3, 4, 2, 8, 9, 7, 6, 5] | ||
>>> max_min_divide_conquer(arr, 0, len(arr) - 1).max | ||
9 | ||
>>> max_min_divide_conquer(arr, 0, len(arr) - 1).min | ||
1 | ||
>>> arr = [7] | ||
>>> max_min_divide_conquer(arr, 0, len(arr) - 1).max | ||
7 | ||
>>> max_min_divide_conquer(arr, 0, len(arr) - 1).min | ||
7 | ||
>>> arr = [7, 1] | ||
>>> max_min_divide_conquer(arr, 0, len(arr) - 1).max | ||
7 | ||
>>> max_min_divide_conquer(arr, 0, len(arr) - 1).min | ||
1 | ||
""" | ||
result = Pair() | ||
|
||
# If only one element in the array | ||
if low == high: | ||
result.max = arr[low] | ||
result.min = arr[low] | ||
return result | ||
|
||
# If there are two elements in the array | ||
if high == low + 1: | ||
if arr[low] < arr[high]: | ||
result.min = arr[low] | ||
result.max = arr[high] | ||
else: | ||
result.min = arr[high] | ||
result.max = arr[low] | ||
return result | ||
|
||
# If there are more than two elements in the array | ||
mid = (low + high) // 2 | ||
left = max_min_divide_conquer(arr, low, mid) | ||
right = max_min_divide_conquer(arr, mid + 1, high) | ||
|
||
# Compare and get the maximum of both parts | ||
result.max = max(left.max, right.max) | ||
|
||
# Compare and get the minimum of both parts | ||
result.min = min(left.min, right.min) | ||
|
||
return result | ||
|
||
|
||
if __name__ == "__main__": | ||
from doctest import testmod | ||
|
||
testmod() |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.