algorithm - How to implement code to find out Maximum Sum Subarray in FORWARD DP style? -


i have tried recurrence relation:

 `f(n) =  f(n+1)+ar[i] if +ve` `     =  0 otherwise` 

but getting wrong answer, understand why giving me wrong after debugging, can't find right solution. me out?

problem statement: given array a, return the

max{sum(any subarray of a)} 

now if traverse array a, each element either part of solution found or start new solution. dp solution be

def max_subarray(a):    max_ending_here = max_so_far = 0    x in a:       max_ending_here = max(0, max_ending_here + x)       max_so_far = max(max_so_far, max_ending_here)    return max_so_far 

now problem can't build recurrence relation same.


Comments