human
I am working on the file "Python/leetcode/easy/strings-arrays.ipynb".
The first task is:
Q.3. Given an integer array nums, find the contiguous subarray (containing at least one number) which has the largest sum and return its sum.
Can you write Python program for it?
assistant
#Here is Python program to find the contiguous subarray (containing at least one number) which has the largest sum and return its sum.

class Solution(object):
    def maxSubArray(self, nums: List[int]) -> int:
        """
        :type nums: List[int]
        :rtype int
        """
        # Special case is when all values in num are negative.
        if max(nums) < 0:
            return max(nums)
        
        max_sum = 0; curr = 0
        for i in range(len(nums)):            
            if curr + nums[i] > 0:
                curr = curr + nums[i]
            else:
                curr = 0 # Reset the sum.
            
            if curr > max_sum:
                max_sum = curr
        
        return max_sum
                

print(Solution().maxSubArray([-2,1,-3,4,-1,2,1,-5,4]))