-
-
Notifications
You must be signed in to change notification settings - Fork 46.9k
Largest subarray sum #1404
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
Merged
Merged
Largest subarray sum #1404
Changes from all commits
Commits
Show all changes
10 commits
Select commit
Hold shift + click to select a range
04a0310
Insertion_sort
anubhav-sharma13 72cf5d2
largest subarray sum
anubhav-sharma13 e0e7ddb
updated print command
anubhav-sharma13 3a2f1af
removed extraspaces
anubhav-sharma13 8247372
removed sys.maxint
anubhav-sharma13 4dd7686
added explaination
anubhav-sharma13 25aaa4b
Updated function style
anubhav-sharma13 c7b4ce0
Update largest_subarray_sum.py
cclauss 844b883
Update i_sort.py
cclauss ff125d0
Delete bogo_bogo_sort.py
cclauss 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,23 @@ | ||
from sys import maxsize | ||
|
||
|
||
def max_sub_array_sum(a: list, size: int = 0): | ||
""" | ||
>>> max_sub_array_sum([-13, -3, -25, -20, -3, -16, -23, -12, -5, -22, -15, -4, -7]) | ||
-3 | ||
""" | ||
size = size or len(a) | ||
max_so_far = -maxsize - 1 | ||
max_ending_here = 0 | ||
for i in range(0, size): | ||
max_ending_here = max_ending_here + a[i] | ||
if max_so_far < max_ending_here: | ||
max_so_far = max_ending_here | ||
if max_ending_here < 0: | ||
max_ending_here = 0 | ||
return max_so_far | ||
|
||
|
||
if __name__ == "__main__": | ||
a = [-13, -3, -25, -20, 1, -16, -23, -12, -5, -22, -15, -4, -7] | ||
print(("Maximum contiguous sum is", max_sub_array_sum(a, len(a)))) |
This file was deleted.
Oops, something went wrong.
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,21 @@ | ||
def insertionSort(arr): | ||
""" | ||
>>> a = arr[:] | ||
>>> insertionSort(a) | ||
>>> a == sorted(a) | ||
True | ||
""" | ||
for i in range(1, len(arr)): | ||
key = arr[i] | ||
j = i - 1 | ||
while j >= 0 and key < arr[j]: | ||
arr[j + 1] = arr[j] | ||
j -= 1 | ||
arr[j + 1] = key | ||
|
||
|
||
arr = [12, 11, 13, 5, 6] | ||
insertionSort(arr) | ||
print("Sorted array is:") | ||
for i in range(len(arr)): | ||
print("%d" % arr[i]) |
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.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Let’s be Pythonic and use
max_sub_array_sum
everywhere.