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] <= 100When you get asked this question in a real-life environment, it will often be ambiguous (especially at FAANG). Make sure to ask these questions in that case:
Build a fresh copy of the grid so the original values stay available while the answer is filled in. For each column, find the largest real value, then write that value into every placeholder in the copy of that column.
Here's how the algorithm would work step-by-step:
class Solution:
def modifiedMatrix(self, matrix):
num_rows = len(matrix)
num_cols = len(matrix[0])
# A new grid is needed to store the results without altering the original during calculation.
answer_matrix = [[0] * num_cols for _ in range(num_rows)]
for row_index in range(num_rows):
for col_index in range(num_cols):
answer_matrix[row_index][col_index] = matrix[row_index][col_index]
# We must iterate column by column to find each column's maximum value independently.
for col_index in range(num_cols):
max_value_in_column = -1
for row_index in range(num_rows):
if matrix[row_index][col_index] > max_value_in_column:
max_value_in_column = matrix[row_index][col_index]
# After finding the max, this second pass through the column updates only the placeholders.
for row_index in range(num_rows):
if matrix[row_index][col_index] == -1:
answer_matrix[row_index][col_index] = max_value_in_column
return answer_matrixThe column maximums can be collected in one pass and stored in a short list, one maximum per column. A second pass then writes each maximum into the placeholder cells of that column, updating the grid in place.
Here's how the algorithm would work step-by-step:
def modified_matrix(matrix):
if not matrix or not matrix[0]:
return matrix
number_of_rows = len(matrix)
number_of_columns = len(matrix[0])
# This array will store the maximum value found in each column to use for replacements later.
column_maximums = [0] * number_of_columns
# The first pass identifies the maximum value in each column.
for column_index in range(number_of_columns):
current_column_max = -1
for row_index in range(number_of_rows):
current_column_max = max(current_column_max, matrix[row_index][column_index])
column_maximums[column_index] = current_column_max
# The second pass modifies the matrix, filling in -1 placeholders with their column's max.
for row_index in range(number_of_rows):
for column_index in range(number_of_columns):
if matrix[row_index][column_index] == -1:
matrix[row_index][column_index] = column_maximums[column_index]
return matrix| Case | How to Handle |
|---|---|
| The grid is a single cell that is already a real value | Leave the cell unchanged, because there is no placeholder to replace. |
| A column contains several placeholders | Every placeholder in that column is replaced with the same column maximum. |
| The maximum is the only non-placeholder in the column | The placeholders still receive that value, since one real value is enough to define the maximum. |
| The grid has a single row | Each placeholder is replaced by the only real value in its column, which is the column maximum. |
| A cell is already equal to the column maximum | It stays as it is, because only placeholder cells are rewritten. |