Skip to content
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.

Commit da34432

Browse files
author
Medha
committedDec 23, 2024·
Add Kadane's Algorithm implementation
1 parent 4abfce2 commit da34432

File tree

3 files changed

+81
-21
lines changed

3 files changed

+81
-21
lines changed
 

‎.pre-commit-config.yaml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,7 @@ repos:
3232
rev: "v2.5.0"
3333
hooks:
3434
- id: pyproject-fmt
35+
language_version: python3.12
3536

3637
- repo: local
3738
hooks:
Lines changed: 56 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,56 @@
1+
class KadaneAlgorithm:
2+
"""
3+
Kadane's Algorithm to find the maximum sum
4+
of a contiguous subarray in a given array.
5+
6+
Time Complexity: O(n)
7+
Space Complexity: O(1)
8+
9+
The function works efficiently with both positive and negative integers.
10+
11+
Usage:
12+
>>> kadane = KadaneAlgorithm()
13+
>>> kadane.max_subarray_sum([1, 2, 3, -2, 5])
14+
9
15+
>>> kadane.max_subarray_sum([-1, -2, -3, -4])
16+
-1
17+
>>> kadane.max_subarray_sum([1, 2, 3, 4])
18+
10
19+
>>> kadane.max_subarray_sum([10, -10, 20, -5, 10])
20+
25
21+
"""
22+
23+
def __init__(self):
24+
pass
25+
26+
def max_subarray_sum(self, arr: list[int]) -> int:
27+
"""
28+
This function finds the maximum sum of a
29+
contiguous subarray using Kadane's Algorithm.
30+
31+
:param arr: List of integers.
32+
:return: Maximum sum of a contiguous subarray.
33+
34+
Raises:
35+
ValueError: If the input array is empty.
36+
37+
>>> kadane = KadaneAlgorithm()
38+
>>> kadane.max_subarray_sum([1, 2, 3, -2, 5])
39+
9
40+
>>> kadane.max_subarray_sum([-1, -2, -3, -4])
41+
-1
42+
>>> kadane.max_subarray_sum([1, 2, 3, 4])
43+
10
44+
>>> kadane.max_subarray_sum([10, -10, 20, -5, 10])
45+
25
46+
"""
47+
if not arr:
48+
raise ValueError("Input array cannot be empty.")
49+
50+
max_sum = current_sum = arr[0]
51+
52+
for num in arr[1:]:
53+
current_sum = max(num, current_sum + num)
54+
max_sum = max(max_sum, current_sum)
55+
56+
return max_sum

‎pyproject.toml

Lines changed: 24 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -49,6 +49,30 @@ euler-validate = [
4949
[tool.ruff]
5050
target-version = "py313"
5151

52+
[tool.codespell]
53+
ignore-words-list = "3rt,ans,bitap,crate,damon,fo,followings,hist,iff,kwanza,manuel,mater,secant,som,sur,tim,toi,zar"
54+
skip = "./.*,*.json,*.lock,ciphers/prehistoric_men.txt,project_euler/problem_022/p022_names.txt,pyproject.toml,strings/dictionary.txt,strings/words.txt"
55+
56+
[tool.pytest.ini_options]
57+
markers = [
58+
"mat_ops: mark a test as utilizing matrix operations.",
59+
]
60+
addopts = [
61+
"--durations=10",
62+
"--doctest-modules",
63+
"--showlocals",
64+
]
65+
66+
[tool.coverage.report]
67+
omit = [
68+
".env/*",
69+
"project_euler/*",
70+
]
71+
sort = "Cover"
72+
73+
[tool.mypy]
74+
python_version = "3.12"
75+
5276
output-format = "full"
5377
lint.select = [
5478
# https://beta.ruff.rs/docs/rules
@@ -158,27 +182,6 @@ lint.pylint.max-branches = 20 # default: 12
158182
lint.pylint.max-returns = 8 # default: 6
159183
lint.pylint.max-statements = 88 # default: 50
160184

161-
[tool.codespell]
162-
ignore-words-list = "3rt,ans,bitap,crate,damon,fo,followings,hist,iff,kwanza,manuel,mater,secant,som,sur,tim,toi,zar"
163-
skip = "./.*,*.json,*.lock,ciphers/prehistoric_men.txt,project_euler/problem_022/p022_names.txt,pyproject.toml,strings/dictionary.txt,strings/words.txt"
164-
165-
[tool.pytest.ini_options]
166-
markers = [
167-
"mat_ops: mark a test as utilizing matrix operations.",
168-
]
169-
addopts = [
170-
"--durations=10",
171-
"--doctest-modules",
172-
"--showlocals",
173-
]
174-
175-
[tool.coverage.report]
176-
omit = [
177-
".env/*",
178-
"project_euler/*",
179-
]
180-
sort = "Cover"
181-
182185
[tool.sphinx-pyproject]
183186
copyright = "2014, TheAlgorithms"
184187
autoapi_dirs = [

0 commit comments

Comments
 (0)
Please sign in to comment.