Problem solution · Python

Maximum Sum of M Non Overlapping Subarrays I

Maximum Sum of M Non Overlapping Subarrays I: a Python solution using breadth-first search. Learn the idea, check the complexity, and read the full code, with credit to Kamyu LeetCode Solutions.

Technique
Breadth-first search
Source
Kamyu LeetCode Solutions
Length
116 lines
Start with the idea.

Try the problem first. If you get stuck, read the approach below, then write your own solution. The full code is at the bottom.

Approach

Breadth-first search

For Maximum Sum of M Non Overlapping Subarrays I, the implementation explores reachable states in layers, which is the standard shape for unweighted shortest paths and minimum-step transitions.

  1. Model each valid configuration as a state and each legal move as an edge.
  2. Seed the queue with the starting state and mark it immediately.
  3. Expand each state once, recording distance or reachability for unseen neighbours.

Code notes

  • 116 lines of Python from the credited upstream file maximum-sum-of-m-non-overlapping-subarrays-i.py.
  • The implementation visibly relies on sequence storage, work queue, cached states.
  • No explicit loop blocks detected.

Complexity

Verify that each state and transition is processed only a bounded number of times; that determines the traversal cost.

Check the problem constraints before deciding whether this complexity will pass.

Source

Code and credit

This code comes from Kamyu LeetCode Solutions by kamyu104 and is used under the MIT licence.

Full codeMaximum Sum of M Non Overlapping Subarrays I · PythonPython
Use this to learn the idea, then write your own version.
# Time:  O(nlogr)# Space: O(n) import collections  # prefix sum, dp, mono deque, wqs binary search, alien trickclass Solution(object):    def maximumSum(self, nums, m, l, r):        """        :type nums: List[int]        :type m: int        :type l: int        :type r: int        :rtype: int        """        NEG_INF = float("-inf")        def binary_search(left, right, check):            while left <= right:                mid = left+(right-left)//2                if check(mid):                    right = mid-1                else:                    left = mid+1            return left         def best_single():            result = NEG_INF            dq = collections.deque()            for i in xrange(1, len(nums)+1):                j = i-l                if j >= 0:                    while dq and prefix[dq[-1]] >= prefix[j]:                        dq.pop()                    dq.append(j)                while dq and dq[0] < i-r:                    dq.popleft()                if dq:                    result = max(result, prefix[i]-prefix[dq[0]])            return result         def f(x):            def better(v1, c1, v2, c2):                return v1 > v2 or (v1 == v2 and c1 < c2)             dp = [[0]*2 for _ in xrange(len(nums)+1)]            dq = collections.deque()            for i in xrange(1, len(nums)+1):                j = i-l                if j >= 0:                    while dq and better(dp[j][0]-prefix[j], dp[j][1], dp[dq[-1]][0]-prefix[dq[-1]], dp[dq[-1]][1]):                        dq.pop()                    dq.append(j)                while dq and dq[0] < i-r:                    dq.popleft()                dp[i] = dp[i-1]                if dq:                    new_dp = [((dp[dq[0]][0]-prefix[dq[0]])+prefix[i])-x, dp[dq[0]][1]+1]                    if better(new_dp[0], new_dp[1], dp[i][0], dp[i][1]):                        dp[i] = new_dp            return dp[-1]         prefix = [0]*(len(nums)+1)        for i in xrange(len(nums)):            prefix[i+1] = prefix[i]+nums[i]        single = best_single()        dp, cnt = f(0)        if not cnt:            return single        if cnt <= m:            return dp        mx = single        assert(f(mx)[1] <= m)        x = binary_search(1, mx, lambda x: f(x)[1] <= m)        return f(x)[0]+m*x  # Time:  O(n * m)# Space: O(n)import collections  # prefix sum, dp, mono dequeclass Solution2(object):    def maximumSum(self, nums, m, l, r):        """        :type nums: List[int]        :type m: int        :type l: int        :type r: int        :rtype: int        """                NEG_INF = float("-inf")        prefix = [0]*(len(nums)+1)        for i in xrange(len(nums)):            prefix[i+1] = prefix[i]+nums[i]        result = NEG_INF        dp = [0]*(len(nums)+1)        for _ in xrange(m):            new_dp = [NEG_INF]*(len(nums)+1)            dq = collections.deque()            for i in xrange(1, len(nums)+1):                new_dp[i] = new_dp[i-1]                j = i-l                if j >= 0 and dp[j] is not NEG_INF:                    while dq and dp[dq[-1]]-prefix[dq[-1]] <= dp[j]-prefix[j]:                        dq.pop()                    dq.append(j)                while dq and dq[0] < i-r:                    dq.popleft()                if dq:                    new_dp[i] = max(new_dp[i], (dp[dq[0]]-prefix[dq[0]])+prefix[i])            dp = new_dp            result = max(result, dp[-1])        return result 

Did this explanation save you time? I'm a Grade 11 student building this free library to make difficult algorithms easier to understand.

Buy me a coffee ↗