Sunday, August 22, 2021

max_subarray

max_subarray 問題 當個紀念


A=[2,4,5,-20,1,2,3,10]


def max_subarray(A):

    

    max_ending_here = max_so_far = A[0]

    print(max_ending_here, max_so_far )

    for x in A[1:]:

        max_ending_here = max(x, max_ending_here + x)

        max_so_far = max(max_so_far, max_ending_here)

        

        print(max_ending_here, max_so_far )

    return max_so_far

    

No comments:

Post a Comment