From 5c2d1fe725ff0281bb1b40efdc2f49461caf1475 Mon Sep 17 00:00:00 2001 From: jbsch Date: Thu, 24 Oct 2024 22:06:42 +0530 Subject: [PATCH 1/3] added largest rectangle histogram function --- .../stacks/largest_rectangle_histogram.py | 39 +++++++++++++++++++ 1 file changed, 39 insertions(+) create mode 100644 data_structures/stacks/largest_rectangle_histogram.py diff --git a/data_structures/stacks/largest_rectangle_histogram.py b/data_structures/stacks/largest_rectangle_histogram.py new file mode 100644 index 000000000..107fb41c4 --- /dev/null +++ b/data_structures/stacks/largest_rectangle_histogram.py @@ -0,0 +1,39 @@ +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 + + >>> largest_rectangle_area([2, 1, 5, 6, 2, 3]) + 10 + + >>> largest_rectangle_area([2, 4]) + 4 + + >>> largest_rectangle_area([6, 2, 5, 4, 5, 1, 6]) + 12 + + >>> largest_rectangle_area([1]) + 1 + """ + stack: list[int] = [] + max_area = 0 + heights = [*heights, 0] # make a new list by appending the sentinel 0 + n = len(heights) + + for i in range(n): + # make sure the stack remains in increasing order + while stack and heights[i] < heights[stack[-1]]: + h = heights[stack.pop()] # height of the bar + # if stack is empty, it means entire width can be taken from index 0 to i-1 + w = i if not stack else i - stack[-1] - 1 # calculate width + max_area = max(max_area, h * w) + + stack.append(i) + + return max_area + + +if __name__ == "__main__": + import doctest + + doctest.testmod() From 50d5bb1af3cfcf45ae7912b3912f76eb74021f18 Mon Sep 17 00:00:00 2001 From: jbsch Date: Thu, 24 Oct 2024 22:13:23 +0530 Subject: [PATCH 2/3] added largest rectangle histogram function --- data_structures/stacks/largest_rectangle_histogram.py | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/data_structures/stacks/largest_rectangle_histogram.py b/data_structures/stacks/largest_rectangle_histogram.py index 107fb41c4..7575bd9f6 100644 --- a/data_structures/stacks/largest_rectangle_histogram.py +++ b/data_structures/stacks/largest_rectangle_histogram.py @@ -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 From bfb816781110a87245858e1e7c7313b10c96dbd8 Mon Sep 17 00:00:00 2001 From: jbsch Date: Thu, 24 Oct 2024 22:39:56 +0530 Subject: [PATCH 3/3] added kadane's algo --- data_structures/arrays/kadanes_algorithm.py | 42 +++++++++++++++++++++ 1 file changed, 42 insertions(+) create mode 100644 data_structures/arrays/kadanes_algorithm.py diff --git a/data_structures/arrays/kadanes_algorithm.py b/data_structures/arrays/kadanes_algorithm.py new file mode 100644 index 000000000..5ab2b1fd1 --- /dev/null +++ b/data_structures/arrays/kadanes_algorithm.py @@ -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()