EPI dynamic programming question - "find max subarray" has incorrect solution

#1

EPI dynamic programming question - “find max subarray” has incorrect solution.

class Solution:
def maxSubArray(self, nums: List[int]) -> int:
minsum = maxsum = nums[0]
for runningsum in itertools. accumulate (nums) :
minsum = min(minsum, runningsum)
maxsum = max(maxsum, runningsum - minsum)
return maxsum

solution dont work for all negative numbers.

input [-1, -2, -2] would return 0 with this solution.

0 Likes