Skip to content

Commit 015523c

Browse files
committed
solved maxsubarray sum
1 parent 0101545 commit 015523c

File tree

1 file changed

+2
-8
lines changed

1 file changed

+2
-8
lines changed

hackerrank/dynamic_programming/maxsubarray.py

Lines changed: 2 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -8,25 +8,19 @@
88

99
# Complete the maxSubarray function below.
1010
def maxSubarray(arr):
11-
max_subseq_sum = None
12-
max_subarr_sum = None
13-
1411
if max(arr) <= 0:
1512
max_subarr_sum = max_subseq_sum = max(arr)
1613
elif min(arr) >= 0:
1714
max_subarr_sum = max_subseq_sum = sum(arr)
1815
else:
1916
max_subarr_sum = max_subarr_ending_here = arr[0]
20-
max_subseq_sum = max_subseq_ending_here = 0
17+
max_subseq_sum = max(arr[0], 0)
2118

2219
for i in range(1, len(arr)):
2320
el = arr[i]
2421
max_subarr_ending_here = max(el, max_subarr_ending_here + el)
2522
max_subarr_sum = max(max_subarr_sum, max_subarr_ending_here)
26-
27-
for el in arr:
28-
max_subseq_ending_here = max(el, 0)
29-
max_subseq_sum += max_subseq_ending_here
23+
max_subseq_sum += max(el, 0)
3024

3125
return [max_subarr_sum, max_subseq_sum]
3226

0 commit comments

Comments
 (0)