Skip to content

Commit 658ae5f

Browse files
committed
feat: add solutions to lc problem: No.1605
No.1605.Find Valid Matrix Given Row and Column Sums
1 parent 1a97a8a commit 658ae5f

File tree

4 files changed

+63
-12
lines changed

4 files changed

+63
-12
lines changed

solution/1600-1699/1605.Find Valid Matrix Given Row and Column Sums/README.md

Lines changed: 24 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -88,6 +88,8 @@
8888

8989
因此,我们把原问题缩小为一个 $m-1$ 行和 $n$ 列的子问题,继续进行上述的操作,直到 $rowSum$ 和 $colSum$ 中的所有元素都为 $0$,就可以得到一个满足题目要求的矩阵 $ans$。
9090

91+
时间复杂度 $O(m \times n)$,空间复杂度 $O(m \times n)$。其中 $m$ 和 $n$ 分别为 $rowSum$ 和 $colSum$ 的长度。
92+
9193
<!-- tabs:start -->
9294

9395
### **Python3**
@@ -189,10 +191,9 @@ func min(a, b int) int {
189191
* @return {number[][]}
190192
*/
191193
var restoreMatrix = function (rowSum, colSum) {
192-
const [m, n] = [rowSum.length, colSum.length];
193-
const ans = Array(m)
194-
.fill(0)
195-
.map(() => Array(n).fill(0));
194+
const m = rowSum.length;
195+
const n = colSum.length;
196+
const ans = Array.from(new Array(m), () => new Array(n).fill(0));
196197
for (let i = 0; i < m; i++) {
197198
for (let j = 0; j < n; j++) {
198199
const x = Math.min(rowSum[i], colSum[j]);
@@ -205,6 +206,25 @@ var restoreMatrix = function (rowSum, colSum) {
205206
};
206207
```
207208

209+
### **TypeScript**
210+
211+
```ts
212+
function restoreMatrix(rowSum: number[], colSum: number[]): number[][] {
213+
const m = rowSum.length;
214+
const n = colSum.length;
215+
const ans = Array.from(new Array(m), () => new Array(n).fill(0));
216+
for (let i = 0; i < m; i++) {
217+
for (let j = 0; j < n; j++) {
218+
const x = Math.min(rowSum[i], colSum[j]);
219+
ans[i][j] = x;
220+
rowSum[i] -= x;
221+
colSum[j] -= x;
222+
}
223+
}
224+
return ans;
225+
}
226+
```
227+
208228
### **...**
209229

210230
```

solution/1600-1699/1605.Find Valid Matrix Given Row and Column Sums/README_EN.md

Lines changed: 22 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -144,10 +144,9 @@ func min(a, b int) int {
144144
* @return {number[][]}
145145
*/
146146
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));
147+
const m = rowSum.length;
148+
const n = colSum.length;
149+
const ans = Array.from(new Array(m), () => new Array(n).fill(0));
151150
for (let i = 0; i < m; i++) {
152151
for (let j = 0; j < n; j++) {
153152
const x = Math.min(rowSum[i], colSum[j]);
@@ -160,6 +159,25 @@ var restoreMatrix = function (rowSum, colSum) {
160159
};
161160
```
162161

162+
### **TypeScript**
163+
164+
```ts
165+
function restoreMatrix(rowSum: number[], colSum: number[]): number[][] {
166+
const m = rowSum.length;
167+
const n = colSum.length;
168+
const ans = Array.from(new Array(m), () => new Array(n).fill(0));
169+
for (let i = 0; i < m; i++) {
170+
for (let j = 0; j < n; j++) {
171+
const x = Math.min(rowSum[i], colSum[j]);
172+
ans[i][j] = x;
173+
rowSum[i] -= x;
174+
colSum[j] -= x;
175+
}
176+
}
177+
return ans;
178+
}
179+
```
180+
163181
### **...**
164182

165183
```

solution/1600-1699/1605.Find Valid Matrix Given Row and Column Sums/Solution.js

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -4,10 +4,9 @@
44
* @return {number[][]}
55
*/
66
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));
7+
const m = rowSum.length;
8+
const n = colSum.length;
9+
const ans = Array.from(new Array(m), () => new Array(n).fill(0));
1110
for (let i = 0; i < m; i++) {
1211
for (let j = 0; j < n; j++) {
1312
const x = Math.min(rowSum[i], colSum[j]);
Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
function restoreMatrix(rowSum: number[], colSum: number[]): number[][] {
2+
const m = rowSum.length;
3+
const n = colSum.length;
4+
const ans = Array.from(new Array(m), () => new Array(n).fill(0));
5+
for (let i = 0; i < m; i++) {
6+
for (let j = 0; j < n; j++) {
7+
const x = Math.min(rowSum[i], colSum[j]);
8+
ans[i][j] = x;
9+
rowSum[i] -= x;
10+
colSum[j] -= x;
11+
}
12+
}
13+
return ans;
14+
}

0 commit comments

Comments
 (0)