40
40
41
41
## Solutions
42
42
43
- ### Solution 1
43
+ ### Solution 1: Simulation
44
+
45
+ We can follow the problem description, traverse each column, find the maximum value of each column, and then traverse each column again, replacing the elements with a value of -1 with the maximum value of that column.
46
+
47
+ The time complexity is $O(m \times n)$, where $m$ and $n$ are the number of rows and columns of the matrix, respectively. The space complexity is $O(1)$.
44
48
45
49
<!-- tabs:start -->
46
50
47
51
``` python
48
52
class Solution :
49
53
def modifiedMatrix (self , matrix : List[List[int ]]) -> List[List[int ]]:
50
- rows = len (matrix)
51
- cols = len (matrix[0 ])
52
- for i in range (cols):
53
- max_val = float (' -inf' )
54
- for j in range (rows):
55
- max_val = max (max_val, matrix[j][i])
56
- for j in range (rows):
57
- if matrix[j][i] == - 1 :
58
- matrix[j][i] = max_val
54
+ m, n = len (matrix), len (matrix[0 ])
55
+ for j in range (n):
56
+ mx = max (matrix[i][j] for i in range (m))
57
+ for i in range (m):
58
+ if matrix[i][j] == - 1 :
59
+ matrix[i][j] = mx
59
60
return matrix
60
61
```
61
62
62
63
``` java
63
64
class Solution {
64
65
public int [][] modifiedMatrix (int [][] matrix ) {
65
- int r = matrix. length;
66
- int c = matrix[0 ]. length;
67
- for (int i = 0 ; i < c; i++ ) {
68
- int maxs = Integer . MIN_VALUE ;
69
- for (int j = 0 ; j < r; j++ ) {
70
- maxs = Math . max(maxs, matrix[j][i]);
66
+ int m = matrix. length, n = matrix[0 ]. length;
67
+ for (int j = 0 ; j < n; ++ j) {
68
+ int mx = - 1 ;
69
+ for (int i = 0 ; i < m; ++ i) {
70
+ mx = Math . max(mx, matrix[i][j]);
71
71
}
72
- for (int j = 0 ; j < r; j ++ ) {
73
- if (matrix[j][i ] == - 1 ) {
74
- matrix[j][i ] = maxs ;
72
+ for (int i = 0 ; i < m; ++ i ) {
73
+ if (matrix[i][j ] == - 1 ) {
74
+ matrix[i][j ] = mx ;
75
75
}
76
76
}
77
77
}
@@ -84,16 +84,15 @@ class Solution {
84
84
class Solution {
85
85
public:
86
86
vector<vector<int >> modifiedMatrix(vector<vector<int >>& matrix) {
87
- int r = matrix.size();
88
- int c = matrix[ 0] .size();
89
- for (int i = 0; i < c; i++) {
90
- int maxs = INT_MIN;
91
- for (int j = 0; j < r; j++) {
92
- maxs = max(maxs, matrix[ j] [ i ] );
87
+ int m = matrix.size(), n = matrix[ 0] .size();
88
+ for (int j = 0; j < n; ++j) {
89
+ int mx = -1;
90
+ for (int i = 0; i < m; ++i) {
91
+ mx = max(mx, matrix[ i] [ j ] );
93
92
}
94
- for (int j = 0; j < r; j++ ) {
95
- if (matrix[ j ] [ i ] == -1) {
96
- matrix[ j ] [ i ] = maxs ;
93
+ for (int i = 0; i < m; ++i ) {
94
+ if (matrix[ i ] [ j ] == -1) {
95
+ matrix[ i ] [ j ] = mx ;
97
96
}
98
97
}
99
98
}
@@ -104,18 +103,15 @@ public:
104
103
105
104
```go
106
105
func modifiedMatrix(matrix [][]int) [][]int {
107
- r := len(matrix)
108
- c := len(matrix[0])
109
- for i := 0; i < c; i++ {
110
- maxs := math.MinInt32
111
- for j := 0; j < r; j++ {
112
- if matrix[j][i] > maxs {
113
- maxs = matrix[j][i]
114
- }
106
+ m, n := len(matrix), len(matrix[0])
107
+ for j := 0; j < n; j++ {
108
+ mx := -1
109
+ for i := 0; i < m; i++ {
110
+ mx = max(mx, matrix[i][j])
115
111
}
116
- for j := 0; j < r; j ++ {
117
- if matrix[j][i ] == -1 {
118
- matrix[j][i ] = maxs
112
+ for i := 0; i < m; i ++ {
113
+ if matrix[i][j ] == -1 {
114
+ matrix[i][j ] = mx
119
115
}
120
116
}
121
117
}
@@ -125,23 +121,42 @@ func modifiedMatrix(matrix [][]int) [][]int {
125
121
126
122
``` ts
127
123
function modifiedMatrix(matrix : number [][]): number [][] {
128
- const rows = matrix .length ;
129
- const cols = matrix [0 ].length ;
130
- for (let i = 0 ; i < cols ; i ++ ) {
131
- let maxVal = Number .MIN_SAFE_INTEGER;
132
- for (let j = 0 ; j < rows ; j ++ ) {
133
- maxVal = Math .max (maxVal , matrix [j ][i ]);
124
+ const [m, n] = [matrix .length , matrix [0 ].length ];
125
+ for (let j = 0 ; j < n ; ++ j ) {
126
+ let mx = - 1 ;
127
+ for (let i = 0 ; i < m ; ++ i ) {
128
+ mx = Math .max (mx , matrix [i ][j ]);
134
129
}
135
- for (let j = 0 ; j < rows ; j ++ ) {
136
- if (matrix [j ][ i ] === - 1 ) {
137
- matrix [j ][ i ] = maxVal ;
130
+ for (let i = 0 ; i < m ; ++ i ) {
131
+ if (matrix [i ][ j ] === - 1 ) {
132
+ matrix [i ][ j ] = mx ;
138
133
}
139
134
}
140
135
}
141
136
return matrix ;
142
137
}
143
138
```
144
139
140
+ ``` cs
141
+ public class Solution {
142
+ public int [][] ModifiedMatrix (int [][] matrix ) {
143
+ int m = matrix .Length , n = matrix [0 ].Length ;
144
+ for (int j = 0 ; j < n ; ++ j ) {
145
+ int mx = - 1 ;
146
+ for (int i = 0 ; i < m ; ++ i ) {
147
+ mx = Math .Max (mx , matrix [i ][j ]);
148
+ }
149
+ for (int i = 0 ; i < m ; ++ i ) {
150
+ if (matrix [i ][j ] == - 1 ) {
151
+ matrix [i ][j ] = mx ;
152
+ }
153
+ }
154
+ }
155
+ return matrix ;
156
+ }
157
+ }
158
+ ```
159
+
145
160
<!-- tabs: end -->
146
161
147
162
<!-- end -->
0 commit comments