Start learning free

LESSON 239 OF 410 · PARTIAL SUMS #2

Maximum Sum of Three Non-Overlapping Subarrays II in Python (Time Complexity: O(n))

Practice O(n3) Read the full explanation

Given an array nums of integers, find three non-overlapping subarrays with maximum sum.

Return the total sum of the three subarrays

Example:

Input: [2, 3, -8, 7, -2, 9, -9, 7, -2, 4]
Output: 28
Explanation: Subarrays [2, 3], [7, -2, 9] and [7, -2, 4]
             have the maximum sum of 28

 

Note:

  • Subarrays must be non-empty
  • nums contains at least three numbers
STEP 1 OF 17
    Ctrl + Enter to check

    PYTHON · PARTIAL SUMS #2

    Max Sum Of Three Subarrays in Python, explained

    Understanding the Problem

    The core challenge of this problem is to find three non-overlapping subarrays that together have the maximum possible sum. This problem is significant in scenarios where we need to maximize the sum of multiple segments of data, such as in financial analysis or signal processing.

    Potential pitfalls include overlapping subarrays and not considering all possible subarray combinations.

    Approach

    To solve this problem, we can break it down into smaller steps:

    1. Calculate the sum of all possible subarrays of a given length.
    2. Use dynamic programming to keep track of the best subarray sums up to each point in the array.
    3. Combine these results to find the maximum sum of three non-overlapping subarrays.

    Let's start with a naive solution and then optimize it.

    Naive Solution

    The naive solution involves checking all possible combinations of three subarrays, which is computationally expensive and not feasible for large arrays.

    Optimized Solution

    We can optimize the solution using dynamic programming:

    1. Calculate the prefix sums of the array to quickly get the sum of any subarray.
    2. Use three arrays to store the maximum sum of subarrays ending at or before each index.
    3. Combine these results to find the maximum sum of three non-overlapping subarrays.

    Algorithm

    Here is a step-by-step breakdown of the optimized algorithm:

    1. Calculate the prefix sums of the array.
    2. Use three arrays to store the maximum sum of subarrays ending at or before each index.
    3. Iterate through the array to find the maximum sum of three non-overlapping subarrays.

    Code Implementation

    def maxSumOfThreeSubarrays(nums, k):
        # Step 1: Calculate the prefix sums
        n = len(nums)
        prefix_sums = [0] * (n + 1)
        for i in range(n):
            prefix_sums[i + 1] = prefix_sums[i] + nums[i]
    
        # Step 2: Initialize arrays to store the maximum sums
        left = [0] * n
        right = [0] * n
        max_sum = 0
    
        # Step 3: Calculate the maximum sum of subarrays ending at or before each index
        for i in range(k - 1, n):
            current_sum = prefix_sums[i + 1] - prefix_sums[i + 1 - k]
            if i == k - 1 or current_sum > prefix_sums[left[i - 1] + k] - prefix_sums[left[i - 1]]:
                left[i] = i + 1 - k
            else:
                left[i] = left[i - 1]
    
        for i in range(n - k, -1, -1):
            current_sum = prefix_sums[i + k] - prefix_sums[i]
            if i == n - k or current_sum >= prefix_sums[right[i + 1] + k] - prefix_sums[right[i + 1]]:
                right[i] = i
            else:
                right[i] = right[i + 1]
    
        # Step 4: Find the maximum sum of three non-overlapping subarrays
        for i in range(k, n - 2 * k + 1):
            l, r = left[i - 1], right[i + k]
            current_sum = (prefix_sums[l + k] - prefix_sums[l] +
                           prefix_sums[i + k] - prefix_sums[i] +
                           prefix_sums[r + k] - prefix_sums[r])
            max_sum = max(max_sum, current_sum)
    
        return max_sum
    
    # Example usage
    nums = [2, 3, -8, 7, -2, 9, -9, 7, -2, 4]
    k = 2
    print(maxSumOfThreeSubarrays(nums, k))  # Output: 28
    

    Complexity Analysis

    The time complexity of this solution is O(n), where n is the length of the input array. This is because we make a constant number of passes through the array. The space complexity is also O(n) due to the additional arrays used for prefix sums and tracking maximum subarray sums.

    Edge Cases

    Potential edge cases include:

    • Arrays with all negative numbers.
    • Arrays with all positive numbers.
    • Arrays with a mix of positive and negative numbers.

    Each of these cases should be tested to ensure the algorithm handles them correctly.

    Testing

    To test the solution comprehensively, consider the following test cases:

    # Test case 1: Mixed positive and negative numbers
    nums = [2, 3, -8, 7, -2, 9, -9, 7, -2, 4]
    k = 2
    assert maxSumOfThreeSubarrays(nums, k) == 28
    
    # Test case 2: All positive numbers
    nums = [1, 2, 3, 4, 5, 6, 7, 8, 9]
    k = 3
    assert maxSumOfThreeSubarrays(nums, k) == 45
    
    # Test case 3: All negative numbers
    nums = [-1, -2, -3, -4, -5, -6, -7, -8, -9]
    k = 2
    assert maxSumOfThreeSubarrays(nums, k) == -12
    
    # Test case 4: Minimum length array
    nums = [1, 2, 3]
    k = 1
    assert maxSumOfThreeSubarrays(nums, k) == 6
    

    Thinking and Problem-Solving Tips

    When approaching such problems, consider breaking them down into smaller, manageable steps. Use dynamic programming to store intermediate results and avoid redundant calculations. Practice similar problems to improve your problem-solving skills.

    Conclusion

    Understanding and solving problems like this one is crucial for developing strong algorithmic thinking. By breaking down the problem and using dynamic programming, we can find an efficient solution. Practice and exploration of similar problems will further enhance your skills.

    Additional Resources

    Keep going with PythonAbout 340 step-by-step lessons, from your first line of code to coding interviews. Free to start.
    Start learning free