mirror of
https://github.com/TheAlgorithms/Python.git
synced 2025-01-30 14:13:44 +00:00
commit
79648e21ff
42
data_structures/arrays/kadanes_algorithm.py
Normal file
42
data_structures/arrays/kadanes_algorithm.py
Normal file
|
@ -0,0 +1,42 @@
|
|||
# Kadane's algorithm
|
||||
|
||||
|
||||
def kadanes_algorithm(arr: list[int]) -> int:
|
||||
"""
|
||||
Function to find the maximum sum of a contiguous subarray using Kadane's algorithm
|
||||
|
||||
>>> kadanes_algorithm([-2, 1, -3, 4, -1, 2, 1, -5, 4])
|
||||
6
|
||||
|
||||
>>> kadanes_algorithm([-1, -2, -3, -4])
|
||||
-1
|
||||
|
||||
>>> kadanes_algorithm([5, 4, -1, 7, 8])
|
||||
23
|
||||
|
||||
>>> kadanes_algorithm([1])
|
||||
1
|
||||
|
||||
>>> kadanes_algorithm([-1, 2, 3, -5, 4])
|
||||
5
|
||||
"""
|
||||
# initializing variables
|
||||
max_current = arr[0] # store the current max sum
|
||||
max_global = arr[0] # store the global max sum
|
||||
|
||||
# looping through the array starting at the second element
|
||||
for i in range(1, len(arr)):
|
||||
# update current max sum by choosing the maximum between
|
||||
# current element alone or current element plus previous max
|
||||
max_current = max(arr[i], max_current + arr[i])
|
||||
|
||||
# update global max sum if current max is larger
|
||||
max_global = max(max_current, max_global)
|
||||
|
||||
return max_global
|
||||
|
||||
|
||||
if __name__ == "__main__":
|
||||
import doctest
|
||||
|
||||
doctest.testmod()
|
|
@ -1,7 +1,7 @@
|
|||
def largest_rectangle_area(heights: list[int]) -> int:
|
||||
"""
|
||||
Given an array of integers representing the heights of bars,
|
||||
this function returns the area of the largest rectangle that can be formed
|
||||
Inputs an array of integers representing the heights of bars,
|
||||
and returns the area of the largest rectangle that can be formed
|
||||
|
||||
>>> largest_rectangle_area([2, 1, 5, 6, 2, 3])
|
||||
10
|
||||
|
|
Loading…
Reference in New Issue
Block a user