-
Notifications
You must be signed in to change notification settings - Fork 4
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
00b6da0
commit a609c4a
Showing
4 changed files
with
37 additions
and
2 deletions.
There are no files selected for viewing
This file contains 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
This file contains 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,9 @@ | ||
def gcd(a: int, b: int) -> int: | ||
""" | ||
Euclidean Algorithm for Greatest Common Divisor (GCD) | ||
Complexity: O(log(min(a, b))) | ||
""" | ||
if a == 0: | ||
return b | ||
return gcd(b % a, a) |
This file contains 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
This file contains 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,26 @@ | ||
# Python program to find maximum contiguous subarray | ||
|
||
from math import inf | ||
from typing import Callable, Optional | ||
|
||
maxint = inf | ||
|
||
|
||
def max_sub_array_sum(array): | ||
""" | ||
Find the sum of contiguous subarray within a one-dimensional | ||
array of numbers that has the largest sum. | ||
""" | ||
max_so_far = -maxint - 1 | ||
max_ending_here = 0 | ||
|
||
for i in range(len(array)): | ||
max_ending_here = max_ending_here + array[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 |