Skip to content

Commit

Permalink
一刷1253
Browse files Browse the repository at this point in the history
  • Loading branch information
diguage committed Sep 26, 2024
1 parent 012d1c0 commit 774c7cf
Show file tree
Hide file tree
Showing 5 changed files with 98 additions and 16 deletions.
16 changes: 8 additions & 8 deletions README.adoc
Original file line number Diff line number Diff line change
Expand Up @@ -8796,14 +8796,14 @@ TIP: **公众号的微信号是: `jikerizhi`**。__因为众所周知的原因
//|{doc_base_url}/1252-cells-with-odd-values-in-a-matrix.adoc[题解]
//|Easy
//|
//
//|{counter:codes}
//|{leetcode_base_url}/reconstruct-a-2-row-binary-matrix/[1253. Reconstruct a 2-Row Binary Matrix^]
//|{source_base_url}/_1253_ReconstructA2RowBinaryMatrix.java[Java]
//|{doc_base_url}/1253-reconstruct-a-2-row-binary-matrix.adoc[题解]
//|Medium
//|
//

|{counter:codes}
|{leetcode_base_url}/reconstruct-a-2-row-binary-matrix/[1253. Reconstruct a 2-Row Binary Matrix^]
|{source_base_url}/_1253_ReconstructA2RowBinaryMatrix.java[Java]
|{doc_base_url}/1253-reconstruct-a-2-row-binary-matrix.adoc[题解]
|Medium
|

//|{counter:codes}
//|{leetcode_base_url}/number-of-closed-islands/[1254. Number of Closed Islands^]
//|{source_base_url}/_1254_NumberOfClosedIslands.java[Java]
Expand Down
28 changes: 24 additions & 4 deletions docs/1253-reconstruct-a-2-row-binary-matrix.adoc
Original file line number Diff line number Diff line change
Expand Up @@ -50,16 +50,36 @@ If no valid solution exists, return an empty 2-D array.
*Constraints:*


* `1 <= colsum.length <= 10^5`
* `0 <= upper, lower <= colsum.length`
* `0 <= colsum[i] <= 2`

* `1 \<= colsum.length \<= 10^5`
* `0 \<= upper, lower \<= colsum.length`
* `0 \<= colsum[i] \<= 2`


== 思路分析

[[src-1253]]
[tabs]
====
一刷::
+
--
[{java_src_attr}]
----
include::{sourcedir}/_1253_ReconstructA2RowBinaryMatrix.java[tag=answer]
----
--
// 二刷::
// +
// --
// [{java_src_attr}]
// ----
// include::{sourcedir}/_1253_ReconstructA2RowBinaryMatrix_2.java[tag=answer]
// ----
// --
====

== 参考资料

. 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 行二进制矩阵 - 官方题解^]

2 changes: 1 addition & 1 deletion docs/index.adoc
Original file line number Diff line number Diff line change
Expand Up @@ -2580,7 +2580,7 @@ include::1109-corporate-flight-bookings.adoc[leveloffset=+1]

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

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

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

Expand Down
10 changes: 7 additions & 3 deletions logbook/202406.adoc
Original file line number Diff line number Diff line change
Expand Up @@ -868,17 +868,21 @@
|{doc_base_url}/2507-smallest-value-after-replacing-with-sum-of-prime-factors.adoc[题解]
|❌ 数学计算,一脸懵逼


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


|{counter:codes}
|{leetcode_base_url}/reordered-power-of-2/[869. Reordered Power of 2^]
|{doc_base_url}/0869-reordered-power-of-2.adoc[题解]
|⭕️ 参考答案做了优化
|⭕️ 数学,参考答案做了优化

|{counter:codes}
|{leetcode_base_url}/reconstruct-a-2-row-binary-matrix/[1253. Reconstruct a 2-Row Binary Matrix^]
|{doc_base_url}/1253-reconstruct-a-2-row-binary-matrix.adoc[题解]
|⭕️ 贪心,为什么需要先减去 2 呢?


|===

Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,58 @@
package com.diguage.algo.leetcode;

import java.util.ArrayList;
import java.util.Collections;
import java.util.List;

public class _1253_ReconstructA2RowBinaryMatrix {
// tag::answer[]
/**
* @author D瓜哥 · https://www.diguage.com
* @since 2024-09-25 21:12:25
*/
public List<List<Integer>> reconstructMatrix(int upper, int lower, int[] colsum) {
int sum = 0;
int twos = 0;
for (int num : colsum) {
sum += num;
if (num == 2) {
twos++;
}
}
if (sum != upper + lower || upper < twos || lower < twos) {
return Collections.emptyList();
}
List<List<Integer>> result = new ArrayList<>(2);
result.add(new ArrayList<>(colsum.length));
result.add(new ArrayList<>(colsum.length));
upper -= twos;
lower -= twos;
for (int i = 0; i < colsum.length; i++) {
int up = 0;
int low = 0;
if (colsum[i] == 2) {
up = 1;
low = 1;
} else if (colsum[i] == 1) {
if (upper > 0) {
up = 1;
upper--;
} else {
low = 1;
lower--;
}
}
result.get(0).add(up);
result.get(1).add(low);
}
return result;
}
// end::answer[]

public static void main(String[] args) {
new _1253_ReconstructA2RowBinaryMatrix()
.reconstructMatrix(5, 5,
new int[]{2, 1, 2, 0, 1, 0, 1, 2, 0, 1});

}
}

0 comments on commit 774c7cf

Please sign in to comment.