Start learning free

LESSON 236 OF 410 · PARTIAL SUMS #2

Maximum Sum Submatrix II in Python (Time Complexity: O(m^2 * n))

Practice O(n2 * m2) Read the full explanation

Given a m x n grid filled with integers, find the submatrix with maximum sum among all submatrices.

Example:

Input:
[
  [ 1,  -9, -10,  1],
  [-1,  10,  10,  1],
  [ 0,   9,   9, -9],
  [-1,  -1,  -1, -1]
]
Output: 38
Explanation: Submatrix [[10, 10], [9, 9]]
STEP 1 OF 17
    Ctrl + Enter to check

    PYTHON · PARTIAL SUMS #2

    Maximum Sum Submatrix in Python, explained

    Understanding the Problem

    The core challenge of this problem is to find the submatrix within a given m x n grid that has the maximum sum. This problem is significant in various applications such as image processing, financial analysis, and more. A common pitfall is to consider only submatrices of fixed sizes or to miss out on potential submatrices that span across different rows and columns.

    Approach

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

    1. First, consider a naive approach where we calculate the sum of all possible submatrices. This approach, however, is not optimal due to its high time complexity.
    2. Next, we can optimize this by using a technique similar to Kadane's algorithm, which is used for finding the maximum sum subarray in a 1D array. We extend this to 2D by fixing the left and right boundaries of the submatrix and then applying Kadane's algorithm on the rows between these boundaries.

    Naive Solution

    The naive solution involves iterating over all possible submatrices and calculating their sums. This approach has a time complexity of O(m^2 * n^2 * m * n), which is impractical for large grids.

    Optimized Solution

    The optimized solution involves the following steps:

    1. Fix the left and right boundaries of the submatrix.
    2. For each pair of boundaries, create a temporary array that stores the sum of elements between these boundaries for each row.
    3. Apply Kadane's algorithm on this temporary array to find the maximum sum subarray, which corresponds to the maximum sum submatrix for the current boundaries.
    4. Repeat the above steps for all pairs of boundaries and keep track of the maximum sum found.

    Algorithm

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

    1. Initialize a variable to store the maximum sum found.
    2. Iterate over all possible left boundaries of the submatrix.
    3. For each left boundary, iterate over all possible right boundaries.
    4. For each pair of boundaries, create a temporary array to store the sum of elements between these boundaries for each row.
    5. Apply Kadane's algorithm on the temporary array to find the maximum sum subarray.
    6. Update the maximum sum if the current sum is greater.
    7. Return the maximum sum found.

    Code Implementation

    def max_sum_submatrix(matrix):
        # Get the dimensions of the matrix
        m, n = len(matrix), len(matrix[0])
        
        # Initialize the maximum sum to a very small number
        max_sum = float('-inf')
        
        # Iterate over all possible left boundaries
        for left in range(n):
            # Initialize a temporary array to store row sums
            temp = [0] * m
            
            # Iterate over all possible right boundaries
            for right in range(left, n):
                # Update the row sums for the current boundaries
                for i in range(m):
                    temp[i] += matrix[i][right]
                
                # Apply Kadane's algorithm to find the maximum sum subarray in temp
                current_sum = kadane(temp)
                
                # Update the maximum sum if the current sum is greater
                max_sum = max(max_sum, current_sum)
        
        return max_sum
    
    def kadane(arr):
        # Initialize variables for Kadane's algorithm
        max_ending_here = max_so_far = arr[0]
        
        # Iterate through the array
        for x in arr[1:]:
            # Update the maximum sum ending at the current position
            max_ending_here = max(x, max_ending_here + x)
            
            # Update the overall maximum sum found so far
            max_so_far = max(max_so_far, max_ending_here)
        
        return max_so_far
    
    # Example usage
    matrix = [
        [ 1,  -9, -10,  1],
        [-1,  10,  10,  1],
        [ 0,   9,   9, -9],
        [-1,  -1,  -1, -1]
    ]
    print(max_sum_submatrix(matrix))  # Output: 38
    

    Complexity Analysis

    The time complexity of the optimized solution is O(m^2 * n). This is because we iterate over all pairs of left and right boundaries (O(n^2)) and for each pair, we apply Kadane's algorithm on an array of size m (O(m)). The space complexity is O(m) for the temporary array used to store row sums.

    Edge Cases

    Potential edge cases include:

    • All elements in the matrix are negative.
    • The matrix contains only one row or one column.
    • The matrix is empty.

    Each of these cases should be handled appropriately by the algorithm. For example, if the matrix is empty, the function should return 0 or an appropriate value indicating no submatrix exists.

    Testing

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

    • A matrix with all positive numbers.
    • A matrix with all negative numbers.
    • A matrix with a mix of positive and negative numbers.
    • A matrix with only one row or one column.
    • An empty matrix.

    Using a testing framework like unittest in Python can help automate and validate these test cases.

    Thinking and Problem-Solving Tips

    When approaching such problems, consider breaking down the problem into smaller, manageable parts. Think about how you can apply known algorithms (like Kadane's algorithm) to solve more complex problems. Practice solving similar problems to improve your problem-solving skills and develop a deeper understanding of different algorithms.

    Conclusion

    In this blog post, we discussed how to find the submatrix with the maximum sum in a given m x n grid. We explored both naive and optimized solutions, provided a detailed algorithm, and implemented the solution in Python. Understanding and solving such problems is crucial for developing strong problem-solving skills and can be applied to various real-world scenarios.

    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