3033. Modify the Matrix LeetCode Solution
In this guide, you will get 3033. Modify the Matrix LeetCode Solution with the best time and space complexity. The solution to Modify the Matrix problem is provided in various programming languages like C++, Java, and Python. This will be helpful for you if you are preparing for placements, hackathons, interviews, or practice purposes. The solutions provided here are very easy to follow and include detailed explanations.
Table of Contents
- Problem Statement
- Complexity Analysis
- Modify the Matrix solution in C++
- Modify the Matrix solution in Java
- Modify the Matrix solution in Python
- Additional Resources

Problem Statement of Modify the Matrix
Given a 0-indexed m x n integer matrix matrix, create a new 0-indexed matrix called answer. Make answer equal to matrix, then replace each element with the value -1 with the maximum element in its respective column.
Return the matrix answer.
Example 1:
Input: matrix = [[1,2,-1],[4,-1,6],[7,8,9]]
Output: [[1,2,9],[4,8,6],[7,8,9]]
Explanation: The diagram above shows the elements that are changed (in blue).
– We replace the value in the cell [1][1] with the maximum value in the column 1, that is 8.
– We replace the value in the cell [0][2] with the maximum value in the column 2, that is 9.
Example 2:
Input: matrix = [[3,-1],[5,2]]
Output: [[3,2],[5,2]]
Explanation: The diagram above shows the elements that are changed (in blue).
Constraints:
m == matrix.length
n == matrix[i].length
2 <= m, n <= 50
-1 <= matrix[i][j] <= 100
The input is generated such that each column contains at least one non-negative integer.
Complexity Analysis
- Time Complexity: O(mn)
- Space Complexity: O(mn)
3033. Modify the Matrix LeetCode Solution in C++
class Solution {
public:
vector<vector<int>> modifiedMatrix(vector<vector<int>>& matrix) {
const int m = matrix.size();
const int n = matrix[0].size();
vector<vector<int>> ans = matrix;
for (int j = 0; j < n; ++j) {
int mx = 0;
for (int i = 0; i < m; ++i)
mx = max(mx, matrix[i][j]);
for (int i = 0; i < m; ++i)
if (matrix[i][j] == -1)
ans[i][j] = mx;
}
return ans;
}
};
/* code provided by PROGIEZ */
3033. Modify the Matrix LeetCode Solution in Java
class Solution {
public int[][] modifiedMatrix(int[][] matrix) {
final int m = matrix.length;
final int n = matrix[0].length;
int[][] ans = matrix.clone();
for (int j = 0; j < n; ++j) {
int mx = 0;
for (int i = 0; i < m; ++i)
mx = Math.max(mx, matrix[i][j]);
for (int i = 0; i < m; ++i)
if (matrix[i][j] == -1)
ans[i][j] = mx;
}
return ans;
}
}
// code provided by PROGIEZ
3033. Modify the Matrix LeetCode Solution in Python
class Solution:
def modifiedMatrix(self, matrix: list[list[int]]) -> list[list[int]]:
m = len(matrix)
n = len(matrix[0])
ans = matrix.copy()
for j in range(n):
mx = max(matrix[i][j] for i in range(m))
for i in range(m):
if matrix[i][j] == -1:
ans[i][j] = mx
return ans
# code by PROGIEZ
Additional Resources
- Explore all LeetCode problem solutions at Progiez here
- Explore all problems on LeetCode website here
Happy Coding! Keep following PROGIEZ for more updates and solutions.