Skip to content

Commit bf3085b

Browse files
committed
一刷1253
1 parent 012d1c0 commit bf3085b

File tree

5 files changed

+99
-15
lines changed

5 files changed

+99
-15
lines changed

README.adoc

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -8796,14 +8796,14 @@ TIP: **公众号的微信号是: `jikerizhi`**。__因为众所周知的原因
87968796
//|{doc_base_url}/1252-cells-with-odd-values-in-a-matrix.adoc[题解]
87978797
//|Easy
87988798
//|
8799-
//
8800-
//|{counter:codes}
8801-
//|{leetcode_base_url}/reconstruct-a-2-row-binary-matrix/[1253. Reconstruct a 2-Row Binary Matrix^]
8802-
//|{source_base_url}/_1253_ReconstructA2RowBinaryMatrix.java[Java]
8803-
//|{doc_base_url}/1253-reconstruct-a-2-row-binary-matrix.adoc[题解]
8804-
//|Medium
8805-
//|
8806-
//
8799+
8800+
|{counter:codes}
8801+
|{leetcode_base_url}/reconstruct-a-2-row-binary-matrix/[1253. Reconstruct a 2-Row Binary Matrix^]
8802+
|{source_base_url}/_1253_ReconstructA2RowBinaryMatrix.java[Java]
8803+
|{doc_base_url}/1253-reconstruct-a-2-row-binary-matrix.adoc[题解]
8804+
|Medium
8805+
|
8806+
88078807
//|{counter:codes}
88088808
//|{leetcode_base_url}/number-of-closed-islands/[1254. Number of Closed Islands^]
88098809
//|{source_base_url}/_1254_NumberOfClosedIslands.java[Java]

docs/1253-reconstruct-a-2-row-binary-matrix.adoc

Lines changed: 25 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -50,16 +50,38 @@ If no valid solution exists, return an empty 2-D array.
5050
*Constraints:*
5151

5252

53-
* `1 <= colsum.length <= 10^5`
54-
* `0 <= upper, lower <= colsum.length`
55-
* `0 <= colsum[i] <= 2`
53+
* `1 \<= colsum.length \<= 10^5`
54+
* `0 \<= upper, lower \<= colsum.length`
55+
* `0 \<= colsum[i] \<= 2`
5656

5757

58+
== 思路分析
5859

60+
为什么需要现在 `upper` 和 `lower` 中先减去 `2` 的数量呢?没想明白
5961

6062
[[src-1253]]
63+
[tabs]
64+
====
65+
一刷::
66+
+
67+
--
6168
[{java_src_attr}]
6269
----
6370
include::{sourcedir}/_1253_ReconstructA2RowBinaryMatrix.java[tag=answer]
6471
----
72+
--
73+
74+
// 二刷::
75+
// +
76+
// --
77+
// [{java_src_attr}]
78+
// ----
79+
// include::{sourcedir}/_1253_ReconstructA2RowBinaryMatrix_2.java[tag=answer]
80+
// ----
81+
// --
82+
====
83+
84+
== 参考资料
85+
86+
. https://leetcode.cn/problems/reconstruct-a-2-row-binary-matrix/solutions/2306023/zhong-gou-2-xing-er-jin-zhi-ju-zhen-by-l-if8v/[1253. 重构 2 行二进制矩阵 - 官方题解^]
6587

docs/index.adoc

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2580,7 +2580,7 @@ include::1109-corporate-flight-bookings.adoc[leveloffset=+1]
25802580

25812581
// include::1252-cells-with-odd-values-in-a-matrix.adoc[leveloffset=+1]
25822582

2583-
// include::1253-reconstruct-a-2-row-binary-matrix.adoc[leveloffset=+1]
2583+
include::1253-reconstruct-a-2-row-binary-matrix.adoc[leveloffset=+1]
25842584

25852585
// include::1254-number-of-closed-islands.adoc[leveloffset=+1]
25862586

logbook/202406.adoc

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -868,17 +868,21 @@
868868
|{doc_base_url}/2507-smallest-value-after-replacing-with-sum-of-prime-factors.adoc[题解]
869869
|❌ 数学计算,一脸懵逼
870870

871-
872871
|{counter:codes}
873872
|{leetcode_base_url}/largest-values-from-labels/[1090. Largest Values From Labels^]
874873
|{doc_base_url}/1090-largest-values-from-labels.adoc[题解]
875874
|⭕️ 贪心。排序规则跟 179、870 题类似:保存下标,使用其他数组的值对下标数组进行排序。
876875

877-
878876
|{counter:codes}
879877
|{leetcode_base_url}/reordered-power-of-2/[869. Reordered Power of 2^]
880878
|{doc_base_url}/0869-reordered-power-of-2.adoc[题解]
881-
|⭕️ 参考答案做了优化
879+
|⭕️ 数学,参考答案做了优化
880+
881+
|{counter:codes}
882+
|{leetcode_base_url}/reconstruct-a-2-row-binary-matrix/[1253. Reconstruct a 2-Row Binary Matrix^]
883+
|{doc_base_url}/1253-reconstruct-a-2-row-binary-matrix.adoc[题解]
884+
|⭕️ 贪心,为什么需要先减去 2 呢?
885+
882886

883887
|===
884888

Lines changed: 58 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,58 @@
1+
package com.diguage.algo.leetcode;
2+
3+
import java.util.ArrayList;
4+
import java.util.Collections;
5+
import java.util.List;
6+
7+
public class _1253_ReconstructA2RowBinaryMatrix {
8+
// tag::answer[]
9+
/**
10+
* @author D瓜哥 · https://www.diguage.com
11+
* @since 2024-09-25 21:12:25
12+
*/
13+
public List<List<Integer>> reconstructMatrix(int upper, int lower, int[] colsum) {
14+
int sum = 0;
15+
int twos = 0;
16+
for (int num : colsum) {
17+
sum += num;
18+
if (num == 2) {
19+
twos++;
20+
}
21+
}
22+
if (sum != upper + lower || upper < twos || lower < twos) {
23+
return Collections.emptyList();
24+
}
25+
List<List<Integer>> result = new ArrayList<>(2);
26+
result.add(new ArrayList<>(colsum.length));
27+
result.add(new ArrayList<>(colsum.length));
28+
upper -= twos;
29+
lower -= twos;
30+
for (int i = 0; i < colsum.length; i++) {
31+
int up = 0;
32+
int low = 0;
33+
if (colsum[i] == 2) {
34+
up = 1;
35+
low = 1;
36+
} else if (colsum[i] == 1) {
37+
if (upper > 0) {
38+
up = 1;
39+
upper--;
40+
} else {
41+
low = 1;
42+
lower--;
43+
}
44+
}
45+
result.get(0).add(up);
46+
result.get(1).add(low);
47+
}
48+
return result;
49+
}
50+
// end::answer[]
51+
52+
public static void main(String[] args) {
53+
new _1253_ReconstructA2RowBinaryMatrix()
54+
.reconstructMatrix(5, 5,
55+
new int[]{2, 1, 2, 0, 1, 0, 1, 2, 0, 1});
56+
57+
}
58+
}

0 commit comments

Comments
 (0)