Minimum Subarray – Python 3 (Week 13 – 09)

class Solution:
    """
    @param: nums: a list of integers
    @return: A integer indicate the sum of minimum subarray
    """
    def minSubArray(self, nums):
        # write your code here
        if not nums:
            return None
        newsum, maxsum = 0, 0
        minsum = nums[0]
        
        for i in range(len(nums)):
            newsum += nums[i]
            if newsum - maxsum < minsum:
                minsum = newsum - maxsum
            if newsum > maxsum:
                maxsum = newsum
            
        return minsum

Time O(n). Space O(1).

Leave a comment

Design a site like this with WordPress.com
Get started