LeetCode 3033: Modify the Matrix (Column Maximum Fill for -1)
LeetCode 3033MatrixSimulationToday we solve LeetCode 3033 - Modify the Matrix.
Source: https://leetcode.com/problems/modify-the-matrix/
English
Problem Summary
You are given a matrix containing normal values and some -1 cells. For each -1, replace it with the maximum value in the same column and return the final matrix.
Key Insight
Every replacement depends only on its column, so we can preprocess the maximum of each column once, then do a second pass to fill all -1 cells.
Algorithm
- Let m be row count and n be column count.
- First pass: compute colMax[j] for each column j.
- Second pass: if matrix[i][j] == -1, set it to colMax[j].
- Return matrix.
Complexity Analysis
Time: O(m*n).
Space: O(n) for column maxima.
Common Pitfalls
- Recomputing column max for each -1 makes it slower.
- Using row max by mistake instead of column max.
- Forgetting to handle multiple -1 in the same column.
Reference Implementations (Java / Go / C++ / Python / JavaScript)
class Solution {
public int[][] modifiedMatrix(int[][] matrix) {
int m = matrix.length, n = matrix[0].length;
int[] colMax = new int[n];
java.util.Arrays.fill(colMax, Integer.MIN_VALUE);
for (int i = 0; i < m; i++) {
for (int j = 0; j < n; j++) {
colMax[j] = Math.max(colMax[j], matrix[i][j]);
}
}
for (int i = 0; i < m; i++) {
for (int j = 0; j < n; j++) {
if (matrix[i][j] == -1) {
matrix[i][j] = colMax[j];
}
}
}
return matrix;
}
}func modifiedMatrix(matrix [][]int) [][]int {
m, n := len(matrix), len(matrix[0])
colMax := make([]int, n)
for j := 0; j < n; j++ {
colMax[j] = -1
}
for i := 0; i < m; i++ {
for j := 0; j < n; j++ {
if matrix[i][j] > colMax[j] {
colMax[j] = matrix[i][j]
}
}
}
for i := 0; i < m; i++ {
for j := 0; j < n; j++ {
if matrix[i][j] == -1 {
matrix[i][j] = colMax[j]
}
}
}
return matrix
}class Solution {
public:
vector<vector<int>> modifiedMatrix(vector<vector<int>>& matrix) {
int m = matrix.size(), n = matrix[0].size();
vector<int> colMax(n, INT_MIN);
for (int i = 0; i < m; i++) {
for (int j = 0; j < n; j++) {
colMax[j] = max(colMax[j], matrix[i][j]);
}
}
for (int i = 0; i < m; i++) {
for (int j = 0; j < n; j++) {
if (matrix[i][j] == -1) {
matrix[i][j] = colMax[j];
}
}
}
return matrix;
}
};class Solution:
def modifiedMatrix(self, matrix: List[List[int]]) -> List[List[int]]:
m, n = len(matrix), len(matrix[0])
col_max = [-1] * n
for i in range(m):
for j in range(n):
col_max[j] = max(col_max[j], matrix[i][j])
for i in range(m):
for j in range(n):
if matrix[i][j] == -1:
matrix[i][j] = col_max[j]
return matrix/**
* @param {number[][]} matrix
* @return {number[][]}
*/
var modifiedMatrix = function(matrix) {
const m = matrix.length, n = matrix[0].length;
const colMax = new Array(n).fill(-1);
for (let i = 0; i < m; i++) {
for (let j = 0; j < n; j++) {
colMax[j] = Math.max(colMax[j], matrix[i][j]);
}
}
for (let i = 0; i < m; i++) {
for (let j = 0; j < n; j++) {
if (matrix[i][j] === -1) {
matrix[i][j] = colMax[j];
}
}
}
return matrix;
};中文
题目概述
给你一个矩阵,里面有普通数值和若干 -1。要求把每个 -1 替换为其所在列的最大值,返回替换后的矩阵。
核心思路
每个位置只依赖“所在列”,所以先统一求每列最大值,再二次遍历把 -1 按列最大值填回去即可。
算法步骤
- 设矩阵大小为 m x n。
- 第一遍遍历:计算每列最大值 colMax[j]。
- 第二遍遍历:若 matrix[i][j] == -1,则改为 colMax[j]。
- 返回矩阵。
复杂度分析
时间复杂度:O(m*n)。
空间复杂度:O(n)(用于列最大值数组)。
常见陷阱
- 每遇到一个 -1 都重新扫描整列,导致重复计算。
- 误用“行最大值”替换而不是“列最大值”。
- 同一列多个 -1 处理不一致。
多语言参考实现(Java / Go / C++ / Python / JavaScript)
class Solution {
public int[][] modifiedMatrix(int[][] matrix) {
int m = matrix.length, n = matrix[0].length;
int[] colMax = new int[n];
java.util.Arrays.fill(colMax, Integer.MIN_VALUE);
for (int i = 0; i < m; i++) {
for (int j = 0; j < n; j++) {
colMax[j] = Math.max(colMax[j], matrix[i][j]);
}
}
for (int i = 0; i < m; i++) {
for (int j = 0; j < n; j++) {
if (matrix[i][j] == -1) {
matrix[i][j] = colMax[j];
}
}
}
return matrix;
}
}func modifiedMatrix(matrix [][]int) [][]int {
m, n := len(matrix), len(matrix[0])
colMax := make([]int, n)
for j := 0; j < n; j++ {
colMax[j] = -1
}
for i := 0; i < m; i++ {
for j := 0; j < n; j++ {
if matrix[i][j] > colMax[j] {
colMax[j] = matrix[i][j]
}
}
}
for i := 0; i < m; i++ {
for j := 0; j < n; j++ {
if matrix[i][j] == -1 {
matrix[i][j] = colMax[j]
}
}
}
return matrix
}class Solution {
public:
vector<vector<int>> modifiedMatrix(vector<vector<int>>& matrix) {
int m = matrix.size(), n = matrix[0].size();
vector<int> colMax(n, INT_MIN);
for (int i = 0; i < m; i++) {
for (int j = 0; j < n; j++) {
colMax[j] = max(colMax[j], matrix[i][j]);
}
}
for (int i = 0; i < m; i++) {
for (int j = 0; j < n; j++) {
if (matrix[i][j] == -1) {
matrix[i][j] = colMax[j];
}
}
}
return matrix;
}
};class Solution:
def modifiedMatrix(self, matrix: List[List[int]]) -> List[List[int]]:
m, n = len(matrix), len(matrix[0])
col_max = [-1] * n
for i in range(m):
for j in range(n):
col_max[j] = max(col_max[j], matrix[i][j])
for i in range(m):
for j in range(n):
if matrix[i][j] == -1:
matrix[i][j] = col_max[j]
return matrix/**
* @param {number[][]} matrix
* @return {number[][]}
*/
var modifiedMatrix = function(matrix) {
const m = matrix.length, n = matrix[0].length;
const colMax = new Array(n).fill(-1);
for (let i = 0; i < m; i++) {
for (let j = 0; j < n; j++) {
colMax[j] = Math.max(colMax[j], matrix[i][j]);
}
}
for (let i = 0; i < m; i++) {
for (let j = 0; j < n; j++) {
if (matrix[i][j] === -1) {
matrix[i][j] = colMax[j];
}
}
}
return matrix;
};
Comments