Skip to content

Commit 309628c

Browse files
committed
feat: add solutions to lc problem: No.1605
No.1605.Find Valid Matrix Given Row and Column Sums
1 parent 14a14e0 commit 309628c

File tree

3 files changed

+76
-0
lines changed

3 files changed

+76
-0
lines changed

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

+31
Original file line numberDiff line numberDiff line change
@@ -76,6 +76,12 @@
7676

7777
<!-- 这里可写通用的实现逻辑 -->
7878

79+
**方法一:贪心 + 构造**
80+
81+
从左上角开始,每次选择行和列中较小的值,作为当前位置的值。即 `ans[i][j] = min(rowSum[i], colSum[j])`
82+
83+
时间复杂度 $O(m \times n)$,其中 $m$ 和 $n$ 分别是 `rowSum``colSum` 的长度。
84+
7985
<!-- tabs:start -->
8086

8187
### **Python3**
@@ -168,6 +174,31 @@ func min(a, b int) int {
168174
}
169175
```
170176

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+
171202
### **...**
172203

173204
```

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

+25
Original file line numberDiff line numberDiff line change
@@ -135,6 +135,31 @@ func min(a, b int) int {
135135
}
136136
```
137137

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+
138163
### **...**
139164

140165
```
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
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+
};

0 commit comments

Comments
 (0)