File tree 3 files changed +76
-0
lines changed
solution/1600-1699/1605.Find Valid Matrix Given Row and Column Sums
3 files changed +76
-0
lines changed Original file line number Diff line number Diff line change 76
76
77
77
<!-- 这里可写通用的实现逻辑 -->
78
78
79
+ ** 方法一:贪心 + 构造**
80
+
81
+ 从左上角开始,每次选择行和列中较小的值,作为当前位置的值。即 ` ans[i][j] = min(rowSum[i], colSum[j]) ` 。
82
+
83
+ 时间复杂度 $O(m \times n)$,其中 $m$ 和 $n$ 分别是 ` rowSum ` 和 ` colSum ` 的长度。
84
+
79
85
<!-- tabs:start -->
80
86
81
87
### ** Python3**
@@ -168,6 +174,31 @@ func min(a, b int) int {
168
174
}
169
175
```
170
176
177
+ ### ** JavaScript**
178
+
179
+ ``` js
180
+ /**
181
+ * @param {number[]} rowSum
182
+ * @param {number[]} colSum
183
+ * @return {number[][]}
184
+ */
185
+ var restoreMatrix = function (rowSum , colSum ) {
186
+ const [m , n ] = [rowSum .length , colSum .length ];
187
+ const ans = Array (m)
188
+ .fill (0 )
189
+ .map (() => Array (n).fill (0 ));
190
+ for (let i = 0 ; i < m; i++ ) {
191
+ for (let j = 0 ; j < n; j++ ) {
192
+ const x = Math .min (rowSum[i], colSum[j]);
193
+ ans[i][j] = x;
194
+ rowSum[i] -= x;
195
+ colSum[j] -= x;
196
+ }
197
+ }
198
+ return ans;
199
+ };
200
+ ```
201
+
171
202
### ** ...**
172
203
173
204
```
Original file line number Diff line number Diff line change @@ -135,6 +135,31 @@ func min(a, b int) int {
135
135
}
136
136
```
137
137
138
+ ### ** JavaScript**
139
+
140
+ ``` js
141
+ /**
142
+ * @param {number[]} rowSum
143
+ * @param {number[]} colSum
144
+ * @return {number[][]}
145
+ */
146
+ var restoreMatrix = function (rowSum , colSum ) {
147
+ const [m , n ] = [rowSum .length , colSum .length ];
148
+ const ans = Array (m)
149
+ .fill (0 )
150
+ .map (() => Array (n).fill (0 ));
151
+ for (let i = 0 ; i < m; i++ ) {
152
+ for (let j = 0 ; j < n; j++ ) {
153
+ const x = Math .min (rowSum[i], colSum[j]);
154
+ ans[i][j] = x;
155
+ rowSum[i] -= x;
156
+ colSum[j] -= x;
157
+ }
158
+ }
159
+ return ans;
160
+ };
161
+ ```
162
+
138
163
### ** ...**
139
164
140
165
```
Original file line number Diff line number Diff line change
1
+ /**
2
+ * @param {number[] } rowSum
3
+ * @param {number[] } colSum
4
+ * @return {number[][] }
5
+ */
6
+ var restoreMatrix = function ( rowSum , colSum ) {
7
+ const [ m , n ] = [ rowSum . length , colSum . length ] ;
8
+ const ans = Array ( m )
9
+ . fill ( 0 )
10
+ . map ( ( ) => Array ( n ) . fill ( 0 ) ) ;
11
+ for ( let i = 0 ; i < m ; i ++ ) {
12
+ for ( let j = 0 ; j < n ; j ++ ) {
13
+ const x = Math . min ( rowSum [ i ] , colSum [ j ] ) ;
14
+ ans [ i ] [ j ] = x ;
15
+ rowSum [ i ] -= x ;
16
+ colSum [ j ] -= x ;
17
+ }
18
+ }
19
+ return ans ;
20
+ } ;
You can’t perform that action at this time.
0 commit comments