Off-by-one on range boundaries
Wrong move: Loop endpoints miss first/last candidate.
Usually fails on: Fails on minimal arrays and exact-boundary answers.
Fix: Re-derive loops from inclusive/exclusive ranges before coding.
Build confidence with an intuition-first walkthrough focused on array fundamentals.
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.lengthn == matrix[i].length2 <= m, n <= 50-1 <= matrix[i][j] <= 100Problem summary: 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.
Start with the most direct exhaustive search. That gives a correctness anchor before optimizing.
Pattern signal: Array
[[1,2,-1],[4,-1,6],[7,8,9]]
[[3,-1],[5,2]]
Source-backed implementations are provided below for direct study and interview prep.
// Accepted solution for LeetCode #3033: Modify the Matrix
class Solution {
public int[][] modifiedMatrix(int[][] matrix) {
int m = matrix.length, n = matrix[0].length;
for (int j = 0; j < n; ++j) {
int mx = -1;
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) {
matrix[i][j] = mx;
}
}
}
return matrix;
}
}
// Accepted solution for LeetCode #3033: Modify the Matrix
func modifiedMatrix(matrix [][]int) [][]int {
m, n := len(matrix), len(matrix[0])
for j := 0; j < n; j++ {
mx := -1
for i := 0; i < m; i++ {
mx = max(mx, matrix[i][j])
}
for i := 0; i < m; i++ {
if matrix[i][j] == -1 {
matrix[i][j] = mx
}
}
}
return matrix
}
# Accepted solution for LeetCode #3033: Modify the Matrix
class Solution:
def modifiedMatrix(self, matrix: List[List[int]]) -> List[List[int]]:
m, n = len(matrix), len(matrix[0])
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:
matrix[i][j] = mx
return matrix
// Accepted solution for LeetCode #3033: Modify the Matrix
// Rust example auto-generated from java reference.
// Replace the signature and local types with the exact LeetCode harness for this problem.
impl Solution {
pub fn rust_example() {
// Port the logic from the reference block below.
}
}
// Reference (java):
// // Accepted solution for LeetCode #3033: Modify the Matrix
// class Solution {
// public int[][] modifiedMatrix(int[][] matrix) {
// int m = matrix.length, n = matrix[0].length;
// for (int j = 0; j < n; ++j) {
// int mx = -1;
// 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) {
// matrix[i][j] = mx;
// }
// }
// }
// return matrix;
// }
// }
// Accepted solution for LeetCode #3033: Modify the Matrix
function modifiedMatrix(matrix: number[][]): number[][] {
const [m, n] = [matrix.length, matrix[0].length];
for (let j = 0; j < n; ++j) {
let mx = -1;
for (let i = 0; i < m; ++i) {
mx = Math.max(mx, matrix[i][j]);
}
for (let i = 0; i < m; ++i) {
if (matrix[i][j] === -1) {
matrix[i][j] = mx;
}
}
}
return matrix;
}
Use this to step through a reusable interview workflow for this problem.
Two nested loops check every pair or subarray. The outer loop fixes a starting point, the inner loop extends or searches. For n elements this gives up to n²/2 operations. No extra space, but the quadratic time is prohibitive for large inputs.
Most array problems have an O(n²) brute force (nested loops) and an O(n) optimal (single pass with clever state tracking). The key is identifying what information to maintain as you scan: a running max, a prefix sum, a hash map of seen values, or two pointers.
Review these before coding to avoid predictable interview regressions.
Wrong move: Loop endpoints miss first/last candidate.
Usually fails on: Fails on minimal arrays and exact-boundary answers.
Fix: Re-derive loops from inclusive/exclusive ranges before coding.