Skip to content

Commit 1f66d36

Browse files
committed
feat: add solutions to lc problem: No.1253
No.1253.Reconstruct a 2-Row Binary Matrix
1 parent a949a02 commit 1f66d36

File tree

7 files changed

+185
-115
lines changed

7 files changed

+185
-115
lines changed

solution/1200-1299/1253.Reconstruct a 2-Row Binary Matrix/README.md

+53-28
Original file line numberDiff line numberDiff line change
@@ -58,18 +58,18 @@
5858

5959
**方法一:贪心**
6060

61-
我们先创建一个答案数组 `ans`,其中 `ans[0]``ans[1]` 分别表示第 $0$ 行和第 $1$ 行的元素
61+
我们先创建一个答案数组 $ans$,其中 $ans[0]$$ans[1]$ 分别表示矩阵的第一行和第二行
6262

63-
对于 `colsum` 中的每个元素 $v$
63+
接下来,从左到右遍历数组 $colsum$,对于当前遍历到的元素 $colsum[j]$,我们有以下几种情况
6464

65-
- 如果 $v = 2$,那么我们将 `ans[0][j]``ans[1][j]` 都置为 $1$,其中 $j$ 是当前元素的下标。此时 `upper``lower` 都减去 $1$。
66-
- 如果 $v = 1$,那么我们将 `ans[0][j]``ans[1][j]` 置为 $1$,其中 $j$ 是当前元素的下标。如果 `upper` 大于 `lower`,那么我们优先将 `ans[0][j]` 置为 $1$,否则我们优先将 `ans[1][j]` 置为 $1$。此时 `upper``lower` 减去 $1$。
67-
- 如果 $v = 0$,那么我们将 `ans[0][j]``ans[1][j]` 都置为 $0$,其中 $j$ 是当前元素的下标
68-
- 如果 `upper``lower` 小于 $0$,那么我们直接返回一个空数组
65+
- 如果 $colsum[j] = 2$,那么我们将 $ans[0][j]$$ans[1][j]$ 都置为 $1$。此时 $upper$$lower$ 都减去 $1$。
66+
- 如果 $colsum[j] = 1$,那么我们将 $ans[0][j]$$ans[1][j]$ 置为 $1$。如果 $upper \gt lower$,那么我们优先将 $ans[0][j]$ 置为 $1$,否则我们优先将 $ans[1][j]$ 置为 $1$。此时 $upper$$lower$ 减去 $1$。
67+
- 如果 $colsum[j] = 0$,那么我们将 $ans[0][j]$$ans[1][j]$ 都置为 $0$。
68+
- 如果 $upper \lt 0$ 或 $lower \lt 0$,那么说明无法构造出满足要求的矩阵,我们返回一个空数组
6969

70-
遍历完 `colsum`,如果 `upper``lower` 都为 $0$,那么我们返回 `ans`,否则我们返回一个空数组。
70+
遍历结束,如果 $upper$$lower$ 都为 $0$,那么我们返回 $ans$,否则我们返回一个空数组。
7171

72-
时间复杂度 $O(n)$,空间复杂度 $O(n)$。其中 $n$ `colsum` 的长度。
72+
时间复杂度 $O(n)$,其中 $n$ 是数组 $colsum$ 的长度。忽略答案数组的空间消耗,空间复杂度 $O(1)$
7373

7474
<!-- tabs:start -->
7575

@@ -108,39 +108,30 @@ class Solution:
108108
class Solution {
109109
public List<List<Integer>> reconstructMatrix(int upper, int lower, int[] colsum) {
110110
int n = colsum.length;
111-
List<List<Integer>> ans = new ArrayList<>();
112111
List<Integer> first = new ArrayList<>();
113112
List<Integer> second = new ArrayList<>();
114113
for (int j = 0; j < n; ++j) {
114+
int a = 0, b = 0;
115115
if (colsum[j] == 2) {
116-
first.add(1);
117-
second.add(1);
116+
a = b = 1;
118117
upper--;
119118
lower--;
120119
} else if (colsum[j] == 1) {
121120
if (upper > lower) {
122121
upper--;
123-
first.add(1);
124-
second.add(0);
122+
a = 1;
125123
} else {
126124
lower--;
127-
first.add(0);
128-
second.add(1);
125+
b = 1;
129126
}
130-
} else {
131-
first.add(0);
132-
second.add(0);
133127
}
134128
if (upper < 0 || lower < 0) {
135-
return ans;
129+
break;
136130
}
131+
first.add(a);
132+
second.add(b);
137133
}
138-
if (upper != 0 || lower != 0) {
139-
return ans;
140-
}
141-
ans.add(first);
142-
ans.add(second);
143-
return ans;
134+
return upper == 0 && lower == 0 ? List.of(first, second) : List.of();
144135
}
145136
}
146137
```
@@ -169,10 +160,10 @@ public:
169160
}
170161
}
171162
if (upper < 0 || lower < 0) {
172-
return {};
163+
break;
173164
}
174165
}
175-
return upper != 0 || lower != 0 ? vector<vector<int>>{} : ans;
166+
return upper || lower ? vector<vector<int>>() : ans;
176167
}
177168
};
178169
```
@@ -202,7 +193,7 @@ func reconstructMatrix(upper int, lower int, colsum []int) [][]int {
202193
}
203194
}
204195
if upper < 0 || lower < 0 {
205-
return [][]int{}
196+
break
206197
}
207198
}
208199
if upper != 0 || lower != 0 {
@@ -212,6 +203,40 @@ func reconstructMatrix(upper int, lower int, colsum []int) [][]int {
212203
}
213204
```
214205

206+
### **TypeScript**
207+
208+
```ts
209+
function reconstructMatrix(
210+
upper: number,
211+
lower: number,
212+
colsum: number[],
213+
): number[][] {
214+
const n = colsum.length;
215+
const ans: number[][] = Array(2)
216+
.fill(0)
217+
.map(() => Array(n).fill(0));
218+
for (let j = 0; j < n; ++j) {
219+
if (colsum[j] === 2) {
220+
ans[0][j] = ans[1][j] = 1;
221+
upper--;
222+
lower--;
223+
} else if (colsum[j] === 1) {
224+
if (upper > lower) {
225+
ans[0][j] = 1;
226+
upper--;
227+
} else {
228+
ans[1][j] = 1;
229+
lower--;
230+
}
231+
}
232+
if (upper < 0 || lower < 0) {
233+
break;
234+
}
235+
}
236+
return upper || lower ? [] : ans;
237+
}
238+
```
239+
215240
### **...**
216241

217242
```

solution/1200-1299/1253.Reconstruct a 2-Row Binary Matrix/README_EN.md

+45-20
Original file line numberDiff line numberDiff line change
@@ -88,39 +88,30 @@ class Solution:
8888
class Solution {
8989
public List<List<Integer>> reconstructMatrix(int upper, int lower, int[] colsum) {
9090
int n = colsum.length;
91-
List<List<Integer>> ans = new ArrayList<>();
9291
List<Integer> first = new ArrayList<>();
9392
List<Integer> second = new ArrayList<>();
9493
for (int j = 0; j < n; ++j) {
94+
int a = 0, b = 0;
9595
if (colsum[j] == 2) {
96-
first.add(1);
97-
second.add(1);
96+
a = b = 1;
9897
upper--;
9998
lower--;
10099
} else if (colsum[j] == 1) {
101100
if (upper > lower) {
102101
upper--;
103-
first.add(1);
104-
second.add(0);
102+
a = 1;
105103
} else {
106104
lower--;
107-
first.add(0);
108-
second.add(1);
105+
b = 1;
109106
}
110-
} else {
111-
first.add(0);
112-
second.add(0);
113107
}
114108
if (upper < 0 || lower < 0) {
115-
return ans;
109+
break;
116110
}
111+
first.add(a);
112+
second.add(b);
117113
}
118-
if (upper != 0 || lower != 0) {
119-
return ans;
120-
}
121-
ans.add(first);
122-
ans.add(second);
123-
return ans;
114+
return upper == 0 && lower == 0 ? List.of(first, second) : List.of();
124115
}
125116
}
126117
```
@@ -149,10 +140,10 @@ public:
149140
}
150141
}
151142
if (upper < 0 || lower < 0) {
152-
return {};
143+
break;
153144
}
154145
}
155-
return upper != 0 || lower != 0 ? vector<vector<int>>{} : ans;
146+
return upper || lower ? vector<vector<int>>() : ans;
156147
}
157148
};
158149
```
@@ -182,7 +173,7 @@ func reconstructMatrix(upper int, lower int, colsum []int) [][]int {
182173
}
183174
}
184175
if upper < 0 || lower < 0 {
185-
return [][]int{}
176+
break
186177
}
187178
}
188179
if upper != 0 || lower != 0 {
@@ -192,6 +183,40 @@ func reconstructMatrix(upper int, lower int, colsum []int) [][]int {
192183
}
193184
```
194185

186+
### **TypeScript**
187+
188+
```ts
189+
function reconstructMatrix(
190+
upper: number,
191+
lower: number,
192+
colsum: number[],
193+
): number[][] {
194+
const n = colsum.length;
195+
const ans: number[][] = Array(2)
196+
.fill(0)
197+
.map(() => Array(n).fill(0));
198+
for (let j = 0; j < n; ++j) {
199+
if (colsum[j] === 2) {
200+
ans[0][j] = ans[1][j] = 1;
201+
upper--;
202+
lower--;
203+
} else if (colsum[j] === 1) {
204+
if (upper > lower) {
205+
ans[0][j] = 1;
206+
upper--;
207+
} else {
208+
ans[1][j] = 1;
209+
lower--;
210+
}
211+
}
212+
if (upper < 0 || lower < 0) {
213+
break;
214+
}
215+
}
216+
return upper || lower ? [] : ans;
217+
}
218+
```
219+
195220
### **...**
196221

197222
```
Original file line numberDiff line numberDiff line change
@@ -1,27 +1,27 @@
1-
class Solution {
2-
public:
3-
vector<vector<int>> reconstructMatrix(int upper, int lower, vector<int>& colsum) {
4-
int n = colsum.size();
5-
vector<vector<int>> ans(2, vector<int>(n));
6-
for (int j = 0; j < n; ++j) {
7-
if (colsum[j] == 2) {
8-
ans[0][j] = ans[1][j] = 1;
9-
upper--;
10-
lower--;
11-
}
12-
if (colsum[j] == 1) {
13-
if (upper > lower) {
14-
upper--;
15-
ans[0][j] = 1;
16-
} else {
17-
lower--;
18-
ans[1][j] = 1;
19-
}
20-
}
21-
if (upper < 0 || lower < 0) {
22-
return {};
23-
}
24-
}
25-
return upper != 0 || lower != 0 ? vector<vector<int>>{} : ans;
26-
}
1+
class Solution {
2+
public:
3+
vector<vector<int>> reconstructMatrix(int upper, int lower, vector<int>& colsum) {
4+
int n = colsum.size();
5+
vector<vector<int>> ans(2, vector<int>(n));
6+
for (int j = 0; j < n; ++j) {
7+
if (colsum[j] == 2) {
8+
ans[0][j] = ans[1][j] = 1;
9+
upper--;
10+
lower--;
11+
}
12+
if (colsum[j] == 1) {
13+
if (upper > lower) {
14+
upper--;
15+
ans[0][j] = 1;
16+
} else {
17+
lower--;
18+
ans[1][j] = 1;
19+
}
20+
}
21+
if (upper < 0 || lower < 0) {
22+
break;
23+
}
24+
}
25+
return upper || lower ? vector<vector<int>>() : ans;
26+
}
2727
};

solution/1200-1299/1253.Reconstruct a 2-Row Binary Matrix/Solution.go

+1-1
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@ func reconstructMatrix(upper int, lower int, colsum []int) [][]int {
2020
}
2121
}
2222
if upper < 0 || lower < 0 {
23-
return [][]int{}
23+
break
2424
}
2525
}
2626
if upper != 0 || lower != 0 {
Original file line numberDiff line numberDiff line change
@@ -1,38 +1,29 @@
1-
class Solution {
2-
public List<List<Integer>> reconstructMatrix(int upper, int lower, int[] colsum) {
3-
int n = colsum.length;
4-
List<List<Integer>> ans = new ArrayList<>();
5-
List<Integer> first = new ArrayList<>();
6-
List<Integer> second = new ArrayList<>();
7-
for (int j = 0; j < n; ++j) {
8-
if (colsum[j] == 2) {
9-
first.add(1);
10-
second.add(1);
11-
upper--;
12-
lower--;
13-
} else if (colsum[j] == 1) {
14-
if (upper > lower) {
15-
upper--;
16-
first.add(1);
17-
second.add(0);
18-
} else {
19-
lower--;
20-
first.add(0);
21-
second.add(1);
22-
}
23-
} else {
24-
first.add(0);
25-
second.add(0);
26-
}
27-
if (upper < 0 || lower < 0) {
28-
return ans;
29-
}
30-
}
31-
if (upper != 0 || lower != 0) {
32-
return ans;
33-
}
34-
ans.add(first);
35-
ans.add(second);
36-
return ans;
37-
}
1+
class Solution {
2+
public List<List<Integer>> reconstructMatrix(int upper, int lower, int[] colsum) {
3+
int n = colsum.length;
4+
List<Integer> first = new ArrayList<>();
5+
List<Integer> second = new ArrayList<>();
6+
for (int j = 0; j < n; ++j) {
7+
int a = 0, b = 0;
8+
if (colsum[j] == 2) {
9+
a = b = 1;
10+
upper--;
11+
lower--;
12+
} else if (colsum[j] == 1) {
13+
if (upper > lower) {
14+
upper--;
15+
a = 1;
16+
} else {
17+
lower--;
18+
b = 1;
19+
}
20+
}
21+
if (upper < 0 || lower < 0) {
22+
break;
23+
}
24+
first.add(a);
25+
second.add(b);
26+
}
27+
return upper == 0 && lower == 0 ? List.of(first, second) : List.of();
28+
}
3829
}

0 commit comments

Comments
 (0)