Skip to content

Commit d44bcb2

Browse files
committed
add maximum subarray program (in python)
1 parent 9629aef commit d44bcb2

1 file changed

Lines changed: 28 additions & 0 deletions

File tree

maximum_subarray.py

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
class Result(object):
2+
3+
def MaxSubArray(self, numbers):
4+
overall_max = float('-inf')
5+
max_ending = 0
6+
7+
for num in numbers:
8+
if max_ending > 0:
9+
max_ending += num
10+
else:
11+
max_ending = num
12+
overall_max = max(overall_max, max_ending)
13+
14+
return overall_max
15+
16+
obj1 = Result()
17+
18+
"""
19+
Example test case -->
20+
Input: nums = [-2,1,-3,4,-1,2,1,-5,4]
21+
"""
22+
23+
print(obj1.MaxSubArray(numbers= [-2,1,-3,4,-1,2,1,-5,4]))
24+
25+
"""
26+
Output: 6
27+
Explanation: [4,-1,2,1] has the largest sum = 6.
28+
"""

0 commit comments

Comments
 (0)