Skip to content
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.

Commit 344ae7d

Browse files
committedApr 21, 2025
Add solution #1605
1 parent 37a3237 commit 344ae7d

File tree

2 files changed

+40
-1
lines changed

2 files changed

+40
-1
lines changed
 

‎README.md

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
# 1,414 LeetCode solutions in JavaScript
1+
# 1,415 LeetCode solutions in JavaScript
22

33
[https://leetcodejavascript.com](https://leetcodejavascript.com)
44

@@ -1239,6 +1239,7 @@
12391239
1601|[Maximum Number of Achievable Transfer Requests](./solutions/1601-maximum-number-of-achievable-transfer-requests.js)|Hard|
12401240
1603|[Design Parking System](./solutions/1603-design-parking-system.js)|Easy|
12411241
1604|[Alert Using Same Key-Card Three or More Times in a One Hour Period](./solutions/1604-alert-using-same-key-card-three-or-more-times-in-a-one-hour-period.js)|Medium|
1242+
1605|[Find Valid Matrix Given Row and Column Sums](./solutions/1605-find-valid-matrix-given-row-and-column-sums.js)|Medium|
12421243
1657|[Determine if Two Strings Are Close](./solutions/1657-determine-if-two-strings-are-close.js)|Medium|
12431244
1668|[Maximum Repeating Substring](./solutions/1668-maximum-repeating-substring.js)|Easy|
12441245
1669|[Merge In Between Linked Lists](./solutions/1669-merge-in-between-linked-lists.js)|Medium|
Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
/**
2+
* 1605. Find Valid Matrix Given Row and Column Sums
3+
* https://leetcode.com/problems/find-valid-matrix-given-row-and-column-sums/
4+
* Difficulty: Medium
5+
*
6+
* You are given two arrays rowSum and colSum of non-negative integers where rowSum[i] is the sum
7+
* of the elements in the ith row and colSum[j] is the sum of the elements of the jth column of a
8+
* 2D matrix. In other words, you do not know the elements of the matrix, but you do know the sums
9+
* of each row and column.
10+
*
11+
* Find any matrix of non-negative integers of size rowSum.length x colSum.length that satisfies
12+
* the rowSum and colSum requirements.
13+
*
14+
* Return a 2D array representing any matrix that fulfills the requirements. It's guaranteed that
15+
* at least one matrix that fulfills the requirements exists.
16+
*/
17+
18+
/**
19+
* @param {number[]} rowSum
20+
* @param {number[]} colSum
21+
* @return {number[][]}
22+
*/
23+
var restoreMatrix = function(rowSum, colSum) {
24+
const rows = rowSum.length;
25+
const cols = colSum.length;
26+
const matrix = Array.from({ length: rows }, () => Array(cols).fill(0));
27+
28+
for (let i = 0; i < rows; i++) {
29+
for (let j = 0; j < cols; j++) {
30+
const value = Math.min(rowSum[i], colSum[j]);
31+
matrix[i][j] = value;
32+
rowSum[i] -= value;
33+
colSum[j] -= value;
34+
}
35+
}
36+
37+
return matrix;
38+
};

0 commit comments

Comments
 (0)
Please sign in to comment.