Max Sum of Rectangle No Larger Than K - Complete Solution Guide
Max Sum of Rectangle No Larger Than K is LeetCode problem 363, a Hard level challenge. This complete guide provides step-by-step explanations, multiple solution approaches, and optimized code in python3, java, cpp, c.
Problem Statement
Given an m x n matrix matrix and an integer k , return the max sum of a rectangle in the matrix such that its sum is no larger than k . It is guaranteed that there will be a rectangle with a sum no larger than k . Example 1: Input: matrix = [[1,0,1],[0,-2,3]], k = 2 Output: 2 Explanation: Because the sum of the blue rectangle [[0, 1], [-2, 3]] is 2, and 2 is the max number no larger than k (k = 2). Example 2: Input: matrix = [[2,2,-1]], k = 3 Output: 3 Constraints: m == matrix.length n == matrix
Detailed Explanation
The problem asks us to find the maximum sum of any rectangle within a given m x n matrix, such that this sum is no larger than a given integer k. The problem guarantees that at least one such rectangle exists. In simpler terms, we want to find a rectangle in the matrix whose sum is as close to k as possible, without exceeding it. The inputs are the 2D matrix and the integer k. The output is the maximum sum of a rectangle that satisfies the condition.
Solution Approach
The solution iterates through all possible pairs of rows (or columns if transposed) in the matrix. For each pair, it creates a 1D array representing the sum of elements between those rows/columns. Then, it uses a binary search approach (implemented with bisect in Python, TreeSet in Java, set in C++, and custom binary search in C) to find the maximum subarray sum within this 1D array that is no larger than k. By comparing all these maximum sums, it determines the overall maximum sum across the entire matrix.
Step-by-Step Algorithm
- Step 1: Determine if the matrix should be transposed (if m > n). This optimizes performance when rows greatly exceed columns.
- Step 2: Iterate through all possible starting rows (i) from 0 to rows - 1.
- Step 3: For each starting row i, iterate through all possible ending rows (j) from i to rows - 1.
- Step 4: Inside the inner loop (j), create or update a 1D array (current_1d_array) representing the column sums between rows i and j.
- Step 5: Call the `find_max_sum_le_k_1d` function (or its equivalent) to find the maximum subarray sum in the 1D array that is no larger than k.
- Step 6: Update the overall `max_val` if the current subarray's maximum sum is greater.
- Step 7: If `max_val` equals k, return k directly (as it cannot be improved further).
- Step 8: After iterating through all possible row pairs, return the `max_val` found.
Key Insights
- Insight 1: The core idea is to iterate through all possible pairs of rows (i, j) in the matrix. For each row pair, calculate the sum of elements in each column between these rows, creating a 1D array.
- Insight 2: The problem then reduces to finding the maximum subarray sum in the 1D array, which is no larger than k. This can be efficiently solved using prefix sums and a binary search (or a balanced tree) to find a suitable prefix sum.
- Insight 3: Consider the case where the number of rows is much larger than the number of columns. Transposing the matrix and processing it that way will improve performance, as it reduces the outer loop iterations.
Complexity Analysis
Time Complexity: O(min(m, n)^2 * max(m, n) * log(max(m, n)))
Space Complexity: O(max(m, n))
Topics
This problem involves: Array, Binary Search, Matrix, Prefix Sum, Ordered Set.
Companies
Asked at: DE Shaw.