Skip to content

Commit 6d79fdd

Browse files
committedApr 7, 2025
Add solution #1253
1 parent bd4a1db commit 6d79fdd

File tree

2 files changed

+64
-1
lines changed

2 files changed

+64
-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,197 LeetCode solutions in JavaScript
1+
# 1,198 LeetCode solutions in JavaScript
22

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

@@ -955,6 +955,7 @@
955955
1249|[Minimum Remove to Make Valid Parentheses](./solutions/1249-minimum-remove-to-make-valid-parentheses.js)|Medium|
956956
1250|[Check If It Is a Good Array](./solutions/1250-check-if-it-is-a-good-array.js)|Hard|
957957
1252|[Cells with Odd Values in a Matrix](./solutions/1252-cells-with-odd-values-in-a-matrix.js)|Easy|
958+
1253|[Reconstruct a 2-Row Binary Matrix](./solutions/1253-reconstruct-a-2-row-binary-matrix.js)|Medium|
958959
1261|[Find Elements in a Contaminated Binary Tree](./solutions/1261-find-elements-in-a-contaminated-binary-tree.js)|Medium|
959960
1267|[Count Servers that Communicate](./solutions/1267-count-servers-that-communicate.js)|Medium|
960961
1268|[Search Suggestions System](./solutions/1268-search-suggestions-system.js)|Medium|
Lines changed: 62 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,62 @@
1+
/**
2+
* 1253. Reconstruct a 2-Row Binary Matrix
3+
* https://leetcode.com/problems/reconstruct-a-2-row-binary-matrix/
4+
* Difficulty: Medium
5+
*
6+
* Given the following details of a matrix with n columns and 2 rows:
7+
* - The matrix is a binary matrix, which means each element in the matrix can be 0 or 1.
8+
* - The sum of elements of the 0-th(upper) row is given as upper.
9+
* - The sum of elements of the 1-st(lower) row is given as lower.
10+
* - The sum of elements in the i-th column(0-indexed) is colsum[i], where colsum is given as an
11+
* integer array with length n.
12+
*
13+
* Your task is to reconstruct the matrix with upper, lower and colsum.
14+
* Return it as a 2-D integer array.
15+
* If there are more than one valid solution, any of them will be accepted.
16+
* If no valid solution exists, return an empty 2-D array.
17+
*/
18+
19+
/**
20+
* @param {number} upper
21+
* @param {number} lower
22+
* @param {number[]} colsum
23+
* @return {number[][]}
24+
*/
25+
var reconstructMatrix = function(upper, lower, colsum) {
26+
const n = colsum.length;
27+
const matrix = [[], []];
28+
let upperLeft = upper;
29+
let lowerLeft = lower;
30+
31+
for (let i = 0; i < n; i++) {
32+
if (colsum[i] === 2) {
33+
matrix[0][i] = 1;
34+
matrix[1][i] = 1;
35+
upperLeft--;
36+
lowerLeft--;
37+
} else {
38+
matrix[0][i] = 0;
39+
matrix[1][i] = 0;
40+
}
41+
}
42+
43+
if (upperLeft < 0 || lowerLeft < 0) return [];
44+
45+
for (let i = 0; i < n; i++) {
46+
if (colsum[i] === 1) {
47+
if (upperLeft > 0) {
48+
matrix[0][i] = 1;
49+
matrix[1][i] = 0;
50+
upperLeft--;
51+
} else if (lowerLeft > 0) {
52+
matrix[0][i] = 0;
53+
matrix[1][i] = 1;
54+
lowerLeft--;
55+
} else {
56+
return [];
57+
}
58+
}
59+
}
60+
61+
return upperLeft === 0 && lowerLeft === 0 ? matrix : [];
62+
};

0 commit comments

Comments
 (0)
Please sign in to comment.