Similar Problems
Similar Problems not available
Maximum Matrix Sum - Leetcode Solution
Companies:
LeetCode: Maximum Matrix Sum Leetcode Solution
Difficulty: Medium
Problem Statement
Given a square matrix of integers mat, we want to maximize the sum of the elements of the matrix subject to the following constraints:
- The absolute value of each element of the matrix must be less than or equal to 1.
- For each row of the matrix, at least one element must be chosen.
- For each column of the matrix, at least one element must be chosen.
We can choose any element of the matrix any number of times, but we cannot change the size or dimensions of the matrix.
Solution
This is a dynamic programming problem. Let's define dp[i][j] as the maximum sum of the square matrix of size i×j that satisfies the given constraints. The problem requires us to find dp[n][n].
If we consider a submatrix of size k×k, then we can compute its maximum sum in O(k^2) time using the Kadane's algorithm, which is a dynamic programming algorithm for finding the maximum subarray sum in a given array.
So, we can use this to compute the maximum sum of all submatrices of size k×k of our main matrix efficiently. Let's define sub[i][j][k] as the maximum sum of the submatrix of size k×k that ends at (i,j) position of the main matrix.
Now, let's think about how to compute dp[i][j] using sub[i][j][k]. We can consider each submatrix of size k×k that completely fits into the i×j main matrix and compute its maximum sum using sub[i][j][k]. The maximum sum of all such submatrices will be the maximum value of dp[i][j].
Formally, we can write:
dp[i][j] = max{dp[i-k][j-k] + sub[i][j][k] | 1 <= k <= min(i,j)}
The above equation computes the maximum sum of all possible submatrices of size k×k that ends at (i,j) position of the main matrix and combines it with the maximum sum of the remaining part of the main matrix, which is dp[i-k][j-k]. We need to add 1 to the last index of the submatrix to satisfy the third constraint.
The total time complexity of this solution is O(n^4), where n is the size of the main matrix. We can optimize it by computing the maximum value of sub[i][j][k] for all free cells (i,j) of the main matrix in O(n^3) time using dynamic programming. The free cells are those that can be chosen for the submatrix ends without violating the first constraint.
Code Implementation
Here is the Python code implementing the above solution:
def maxMatrixSum(mat: List[List[int]]) -> int: n = len(mat) dp = [[0] * (n+1) for _ in range(n+1)] sub = [[[0]*(n+1) for _ in range(n+1)] for _ in range(2)] mx = 0 for i in range(n): for j in range(n): sub[0][i][j] = sub[0][i][j-1] + mat[i][j] if j > 0 else mat[i][j] sub[1][i][j] = sub[1][i-1][j] + sub[0][i][j] if abs(mat[i][j]) > 1: mx += abs(mat[i][j]) - 1 else: dp[i+1][j+1] = mat[i][j] for k in range(2, n+1): for i in range(n-k+1): for j in range(n-k+1): subsum = sub[1][i+k-1][j+k-1] - sub[1][i+k-1][j-1] - sub[1][i-1][j+k-1] + sub[1][i-1][j-1] dp[i+k][j+k] = max(dp[i+k][j+k], dp[i][j] + subsum) return dp[n][n] + mx
The code first initializes dp and sub matrices with zeros. It then computes sub matrices of size 1×1 for all cells of the main matrix and updates dp matrix accordingly. It also calculates the initial value of mx for the given matrix.
Then, it computes sub matrices of size k×k for all free cells of the main matrix and calculates the maximum sum of all such sub matrices using the equation given above. Finally, it returns the maximum sum of the main matrix and the value of mx.
Conclusion
We have discussed the detailed solution of the Maximum Matrix Sum problem on LeetCode. This problem requires dynamic programming approach, and we can use the Kadane's algorithm to compute the maximum sum of submatrices of a given size efficiently. We also need to compute the maximum value of submatrices for the free cells of the main matrix to optimize the solution.
Maximum Matrix Sum Solution Code
1