From 02e68905d4002ba00f155b9541bc909cb4d39851 Mon Sep 17 00:00:00 2001 From: Aniketsy <148300120+Aniketsy@users.noreply.github.com> Date: Sun, 6 Oct 2024 00:34:04 +0530 Subject: [PATCH 1/3] Added Kadane's Algorithm for Maximum Subarray Sum --- data_structures/arrays/Maximum Subarray.py | 73 ++++++++++++++++++++++ 1 file changed, 73 insertions(+) create mode 100644 data_structures/arrays/Maximum Subarray.py diff --git a/data_structures/arrays/Maximum Subarray.py b/data_structures/arrays/Maximum Subarray.py new file mode 100644 index 000000000000..0bcc3ea6208f --- /dev/null +++ b/data_structures/arrays/Maximum Subarray.py @@ -0,0 +1,73 @@ +""" +Find the Maximum Subarray Sum using Kadane's Algorithm. +Reference: https://leetcode.com/problems/maximum-subarray/ + +Python doctest can be run with the following command: +python -m doctest -v maximum_subarray.py + +Given an integer array nums, this function returns +the maximum sum of a contiguous subarray. + +A subarray is a contiguous part of an array. + +Example Input: +nums = [-2, 1, -3, 4, -1, 2, 1, -5, 4] +Output: 6 + +""" + +def max_subarray_sum(nums: list[int]) -> int: + """ + Find the maximum subarray sum using Kadane's Algorithm. + + Args: + nums (list[int]): The input array of integers. + + Returns: + int: The maximum subarray sum. + + Examples: + >>> max_subarray_sum([-2, 1, -3, 4, -1, 2, 1, -5, 4]) + 6 + >>> max_subarray_sum([1]) + 1 + >>> max_subarray_sum([5, 4, -1, 7, 8]) + 23 + >>> max_subarray_sum([-1, -2, -3, -4]) + -1 + """ + max_current = max_global = nums[0] + + for num in nums[1:]: + max_current = max(num, max_current + num) + max_global = max(max_global, max_current) + + return max_global + +if __name__ == "__main__": + import doctest + + doctest.testmod() + + +# Kadane's Algorithm + +""" +Kadane's Algorithm is an efficient method to find the maximum +sum of a contiguous subarray within a one-dimensional array of +numbers. It maintains two key values as we traverse the array: +the current maximum sum ending at the current index and the +global maximum sum found so far. +""" +# Advantages +""" +- Efficiency**: Runs in linear time, `O(n)`. +- Simplicity**: Straightforward to implement and understand. +- Versatility**: Easily adaptable to related problems. +""" + +### Time Complexity +""" +- Time Complexity**: `O(n)` - processes each element once. +- Space Complexity**: `O(1)` - uses a fixed amount of extra space. +""" From ff037fc9a75c3af81408765106ba0dcc2970b07c Mon Sep 17 00:00:00 2001 From: Aniketsy <148300120+Aniketsy@users.noreply.github.com> Date: Sun, 6 Oct 2024 00:40:53 +0530 Subject: [PATCH 2/3] Added Kadane's Algorithm for Maximum Subarray Sum --- .../arrays/{Maximum Subarray.py => maximum_subarray.py} | 0 1 file changed, 0 insertions(+), 0 deletions(-) rename data_structures/arrays/{Maximum Subarray.py => maximum_subarray.py} (100%) diff --git a/data_structures/arrays/Maximum Subarray.py b/data_structures/arrays/maximum_subarray.py similarity index 100% rename from data_structures/arrays/Maximum Subarray.py rename to data_structures/arrays/maximum_subarray.py From ff6a17d5a43bf20112bc25ce9571c69829218ddc Mon Sep 17 00:00:00 2001 From: Aniketsy <148300120+Aniketsy@users.noreply.github.com> Date: Sun, 6 Oct 2024 00:56:11 +0530 Subject: [PATCH 3/3] Added Kadane's Algorithm for Maximum Subarray Sum --- data_structures/arrays/maximum_subarray.py | 50 +++++++++++----------- 1 file changed, 24 insertions(+), 26 deletions(-) diff --git a/data_structures/arrays/maximum_subarray.py b/data_structures/arrays/maximum_subarray.py index 0bcc3ea6208f..a317ef94f6ee 100644 --- a/data_structures/arrays/maximum_subarray.py +++ b/data_structures/arrays/maximum_subarray.py @@ -1,3 +1,24 @@ +""" +Kadane's Algorithm is an efficient method to find the maximum +sum of a contiguous subarray within a one-dimensional array of +numbers. It maintains two key values as we traverse the array: +the current maximum sum ending at the current index and the +global maximum sum found so far. +""" + +# Advantages +""" +- Efficiency: Runs in linear time, `O(n)`. +- Simplicity: Straightforward to implement and understand. +- Versatility: Easily adaptable to related problems. +""" + +# Time Complexity +""" +- Time Complexity: `O(n)` - processes each element once. +- Space Complexity: `O(1)` - uses a fixed amount of extra space. +""" + """ Find the Maximum Subarray Sum using Kadane's Algorithm. Reference: https://leetcode.com/problems/maximum-subarray/ @@ -13,7 +34,6 @@ Example Input: nums = [-2, 1, -3, 4, -1, 2, 1, -5, 4] Output: 6 - """ def max_subarray_sum(nums: list[int]) -> int: @@ -37,37 +57,15 @@ def max_subarray_sum(nums: list[int]) -> int: -1 """ max_current = max_global = nums[0] - + for num in nums[1:]: max_current = max(num, max_current + num) max_global = max(max_global, max_current) return max_global + if __name__ == "__main__": import doctest - doctest.testmod() - - -# Kadane's Algorithm - -""" -Kadane's Algorithm is an efficient method to find the maximum -sum of a contiguous subarray within a one-dimensional array of -numbers. It maintains two key values as we traverse the array: -the current maximum sum ending at the current index and the -global maximum sum found so far. -""" -# Advantages -""" -- Efficiency**: Runs in linear time, `O(n)`. -- Simplicity**: Straightforward to implement and understand. -- Versatility**: Easily adaptable to related problems. -""" - -### Time Complexity -""" -- Time Complexity**: `O(n)` - processes each element once. -- Space Complexity**: `O(1)` - uses a fixed amount of extra space. -""" + doctest.testmod() \ No newline at end of file