File tree Expand file tree Collapse file tree 1 file changed +2
-8
lines changed
hackerrank/dynamic_programming Expand file tree Collapse file tree 1 file changed +2
-8
lines changed Original file line number Diff line number Diff line change 8
8
9
9
# Complete the maxSubarray function below.
10
10
def maxSubarray (arr ):
11
- max_subseq_sum = None
12
- max_subarr_sum = None
13
-
14
11
if max (arr ) <= 0 :
15
12
max_subarr_sum = max_subseq_sum = max (arr )
16
13
elif min (arr ) >= 0 :
17
14
max_subarr_sum = max_subseq_sum = sum (arr )
18
15
else :
19
16
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 )
21
18
22
19
for i in range (1 , len (arr )):
23
20
el = arr [i ]
24
21
max_subarr_ending_here = max (el , max_subarr_ending_here + el )
25
22
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 )
30
24
31
25
return [max_subarr_sum , max_subseq_sum ]
32
26
You can’t perform that action at this time.
0 commit comments