Skip to content

Commit 6440664

Browse files
authored
Added Kadane algorithm for max_contiguous_subsequence_sum problem with requested changes. (#861)
* initial commit * initial commit! * Added Kadane's Algorithm for max_contiguous_subsequence_sum problem! * Update README.md * Update max_contiguous_subsequence_sum.py * Update max_contiguous_subsequence_sum.py * fix #854
1 parent 3259a07 commit 6440664

File tree

4 files changed

+66
-0
lines changed

4 files changed

+66
-0
lines changed

README.md

+2
Original file line numberDiff line numberDiff line change
@@ -71,6 +71,8 @@ If you want to uninstall algorithms, it is as simple as:
7171
- [two_sum](algorithms/arrays/two_sum.py)
7272
- [move_zeros](algorithms/arrays/move_zeros.py)
7373
- [n_sum](algorithms/arrays/n_sum.py)
74+
- [greedy](algorithms/greedy/)
75+
- [max_contiguous_subsequence_sum](algorithms/greedy/max_contiguous_subsequence_sum.py)
7476
- [automata](algorithms/automata)
7577
- [DFA](algorithms/automata/dfa.py)
7678
- [backtrack](algorithms/backtrack)

algorithms/greedy/__init__.py

+1
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
from .max_contiguous_subsequence_sum import *
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
1+
'''
2+
Algorithm used => Kadane's Algorithm
3+
4+
kadane's algorithm is used for finding the maximum sum of contiguous subsequence in a sequence.
5+
It is considered a greedy/dp algorithm but I think they more greedy than dp
6+
here are some of the examples to understand the use case more clearly
7+
Example1 => [-2, 3, 8, -1, 4]
8+
result => {3, 8, -1, 4} => 14
9+
Example2 => [-1, 1, 0]
10+
result => {1} => 1
11+
Example3 => [-1, -3, -4]
12+
result => -1
13+
Example1 => [-2, 3, 8, -12, 8, 4]
14+
result => {8, 4} => 12
15+
Basic Algorithm Idea
16+
If the sum of the current contiguous subsequence after adding the value at the current position is less than the value
17+
at the current position then we know that it will be better if we start the current contiguous subsequence from this position.
18+
Else we add the value at the current position to the current contiguous subsequence.
19+
Note
20+
In the implementation, the contiguous subsequence has at least one element.
21+
If it can have 0 elements then the result will be max(max_till_now, 0)
22+
'''
23+
24+
25+
def max_contiguous_subsequence_sum(arr) -> int:
26+
arr_size = len(arr)
27+
28+
if arr_size == 0:
29+
return 0
30+
31+
max_till_now = arr[0]
32+
curr_sub_sum = 0
33+
34+
for i in range(0, arr_size):
35+
if curr_sub_sum + arr[i] < arr[i]:
36+
curr_sub_sum = arr[i]
37+
else:
38+
curr_sub_sum += arr[i]
39+
40+
max_till_now = max(max_till_now, curr_sub_sum)
41+
42+
return max_till_now

tests/test_greedy.py

+21
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
from algorithms.greedy import (
2+
max_contiguous_subsequence_sum,
3+
)
4+
5+
import unittest
6+
7+
class TestMaxContiguousSubsequenceSum(unittest.TestCase):
8+
def test_max_contiguous_subsequence_sum(self):
9+
arr1 = [-2, 3, 8, -1, 4]
10+
arr2 = [-1, 1, 0]
11+
arr3 = [-1, -3, -4]
12+
arr4 = [-2, 3, 8, -12, 8, 4]
13+
14+
self.assertEqual(max_contiguous_subsequence_sum(arr1), 14)
15+
self.assertEqual(max_contiguous_subsequence_sum(arr2), 1)
16+
self.assertEqual(max_contiguous_subsequence_sum(arr3), -1)
17+
self.assertEqual(max_contiguous_subsequence_sum(arr4), 12)
18+
19+
if __name__ == '__main__':
20+
21+
unittest.main()

0 commit comments

Comments
 (0)