|
| 1 | +--- |
| 2 | +id: modify-the-matrix |
| 3 | +title: Modify the Matrix (LeetCode) |
| 4 | +sidebar_label: 3033-ModifyTheMatrix |
| 5 | +tags: |
| 6 | + - Matrix |
| 7 | + - Array |
| 8 | +description: Modify a matrix by replacing each element with the value -1 with the maximum element in its respective column. |
| 9 | +sidebar_position: 3033 |
| 10 | +--- |
| 11 | + |
| 12 | +## Problem Description |
| 13 | + |
| 14 | +| Problem Statement | Solution Link | LeetCode Profile | |
| 15 | +| :---------------- | :------------ | :--------------- | |
| 16 | +| [Modify the Matrix](https://leetcode.com/problems/modify-the-matrix/) | [Modify the Matrix Solution on LeetCode](https://leetcode.com/problems/modify-the-matrix/solutions/) | [vaishu_1904](https://leetcode.com/u/vaishu_1904/) | |
| 17 | + |
| 18 | +## Problem Description |
| 19 | + |
| 20 | +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. |
| 21 | + |
| 22 | +Return the matrix answer. |
| 23 | + |
| 24 | +### Example 1 |
| 25 | + |
| 26 | +- **Input:** `matrix = [[1,2,-1],[4,-1,6],[7,8,9]]` |
| 27 | +- **Output:** `[[1,2,9],[4,8,6],[7,8,9]]` |
| 28 | +- **Explanation:** The diagram above shows the elements that are changed (in blue). |
| 29 | + - We replace the value in the cell [1][1] with the maximum value in the column 1, that is 8. |
| 30 | + - We replace the value in the cell [0][2] with the maximum value in the column 2, that is 9. |
| 31 | + |
| 32 | +### Example 2 |
| 33 | + |
| 34 | +- **Input:** `matrix = [[3,-1],[5,2]]` |
| 35 | +- **Output:** `[[3,2],[5,2]]` |
| 36 | +- **Explanation:** The diagram above shows the elements that are changed (in blue). |
| 37 | + |
| 38 | +### Constraints |
| 39 | + |
| 40 | +- `m == matrix.length` |
| 41 | +- `n == matrix[i].length` |
| 42 | +- `2 <= m, n <= 50` |
| 43 | +- `-1 <= matrix[i][j] <= 100` |
| 44 | +- The input is generated such that each column contains at least one non-negative integer. |
| 45 | + |
| 46 | +## Approach |
| 47 | + |
| 48 | +To solve this problem, we need to replace each occurrence of `-1` in the matrix with the maximum value in its respective column. Here are the steps: |
| 49 | + |
| 50 | +1. Traverse each column of the matrix to find the maximum value. |
| 51 | +2. Traverse the matrix again to replace each `-1` with the corresponding column maximum value. |
| 52 | + |
| 53 | +### Solution Code |
| 54 | + |
| 55 | +#### Python |
| 56 | + |
| 57 | +```python |
| 58 | +class Solution: |
| 59 | + def modifiedMatrix(self, matrix: List[List[int]]) -> List[List[int]]: |
| 60 | + m, n = len(matrix), len(matrix[0]) |
| 61 | + col_max = [max(matrix[i][j] for i in range(m) if matrix[i][j] != -1) for j in range(n)] |
| 62 | + |
| 63 | + for i in range(m): |
| 64 | + for j in range(n): |
| 65 | + if matrix[i][j] == -1: |
| 66 | + matrix[i][j] = col_max[j] |
| 67 | + |
| 68 | + return matrix |
| 69 | +``` |
| 70 | + |
| 71 | +#### C++ |
| 72 | +```c++ |
| 73 | +#include <vector> |
| 74 | +#include <algorithm> |
| 75 | +using namespace std; |
| 76 | + |
| 77 | +class Solution { |
| 78 | +public: |
| 79 | + vector<vector<int>> modifiedMatrix(vector<vector<int>>& matrix) { |
| 80 | + int m = matrix.size(), n = matrix[0].size(); |
| 81 | + vector<int> colMax(n, INT_MIN); |
| 82 | + |
| 83 | + for (int j = 0; j < n; ++j) { |
| 84 | + for (int i = 0; i < m; ++i) { |
| 85 | + if (matrix[i][j] != -1) { |
| 86 | + colMax[j] = max(colMax[j], matrix[i][j]); |
| 87 | + } |
| 88 | + } |
| 89 | + } |
| 90 | + |
| 91 | + for (int i = 0; i < m; ++i) { |
| 92 | + for (int j = 0; j < n; ++j) { |
| 93 | + if (matrix[i][j] == -1) { |
| 94 | + matrix[i][j] = colMax[j]; |
| 95 | + } |
| 96 | + } |
| 97 | + } |
| 98 | + |
| 99 | + return matrix; |
| 100 | + } |
| 101 | +}; |
| 102 | + |
| 103 | +``` |
| 104 | +
|
| 105 | +#### Java |
| 106 | +```java |
| 107 | +class Solution { |
| 108 | + public int[][] modifiedMatrix(int[][] matrix) { |
| 109 | + int m = matrix.length, n = matrix[0].length; |
| 110 | + int[] colMax = new int[n]; |
| 111 | + Arrays.fill(colMax, Integer.MIN_VALUE); |
| 112 | + |
| 113 | + for (int j = 0; j < n; ++j) { |
| 114 | + for (int i = 0; i < m; ++i) { |
| 115 | + if (matrix[i][j] != -1) { |
| 116 | + colMax[j] = Math.max(colMax[j], matrix[i][j]); |
| 117 | + } |
| 118 | + } |
| 119 | + } |
| 120 | + |
| 121 | + for (int i = 0; i < m; ++i) { |
| 122 | + for (int j = 0; j < n; ++j) { |
| 123 | + if (matrix[i][j] == -1) { |
| 124 | + matrix[i][j] = colMax[j]; |
| 125 | + } |
| 126 | + } |
| 127 | + } |
| 128 | + |
| 129 | + return matrix; |
| 130 | + } |
| 131 | +} |
| 132 | +
|
| 133 | +``` |
| 134 | + |
| 135 | +### Conclusion |
| 136 | +The solutions traverse the matrix twice to efficiently find the maximum values for each column and |
| 137 | +then replace the -1 values. This approach ensures that the problem is solved in a straightforward |
| 138 | +and efficient manner across different programming languages. |
0 commit comments