Movatterモバイル変換


[0]ホーム

URL:


Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commit5f09974

Browse files
authored
Create kadane.md
1 parent0b7cbcf commit5f09974

File tree

1 file changed

+36
-0
lines changed

1 file changed

+36
-0
lines changed

‎docs/subarray/kadane.md‎

Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
#Kadane's Algorithm
2+
3+
Kadane’s algorithm is to look for all positive contiguous segments of the array (max_ending_here is used for this). And keep track of maximum sum contiguous segment among all positive segments (max_so_far is used for this) Each time we get a positive sum compare it with max_so_far and update max_so_far if it is greater than max_so_far
4+
##Install
5+
6+
```
7+
pip install allalgorithms
8+
```
9+
10+
##Usage
11+
12+
```py
13+
from allalgorithms.subarrayimport kadane
14+
15+
arr= [-2,1,2,7,10,77]
16+
17+
print(kadane.maxsum_subarray(arr))
18+
# -> 97
19+
20+
print(kadane.returnArray(arr))
21+
# -> [97, [1, 2, 7, 10, 77]]
22+
```
23+
24+
##API
25+
26+
###maxsum_subarray(array)
27+
28+
>Return sum of maximum contigious subarray
29+
30+
###returnArray(array)
31+
32+
>Return sum along with the respective subarray
33+
34+
#####Params:
35+
36+
-`array`: Array (may contain negative elements)

0 commit comments

Comments
 (0)

[8]ページ先頭

©2009-2025 Movatter.jp