Skip to content

Commit 065e8cc

Browse files
authored
Merge pull request #1079 from 0xff-dev/1253
Add solution and test-cases for problem 1253
2 parents 811bb24 + 06fe0f4 commit 065e8cc

File tree

3 files changed

+83
-26
lines changed

3 files changed

+83
-26
lines changed

leetcode/1201-1300/1253.Reconstruct-a-2-Row-Binary-Matrix/README.md

Lines changed: 27 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -1,28 +1,42 @@
11
# [1253.Reconstruct a 2-Row Binary Matrix][title]
22

3-
> [!WARNING|style:flat]
4-
> This question is temporarily unanswered if you have good ideas. Welcome to [Create Pull Request PR](https://github.com/kylesliu/awesome-golang-algorithm)
5-
63
## Description
4+
Given the following details of a matrix with `n` columns and `2` rows :
5+
6+
- The matrix is a binary matrix, which means each element in the matrix can be `0` or `1`.
7+
- The sum of elements of the 0-th(upper) row is given as `upper`.
8+
- The sum of elements of the 1-st(lower) row is given as `lower`.
9+
- The sum of elements in the i-th column(0-indexed) is `colsum[i]`, where `colsum` is given as an integer array with length `n`.
10+
11+
Your task is to reconstruct the matrix with `upper`, `lower` and `colsum`.
12+
13+
Return it as a 2-D integer array.
14+
15+
If there are more than one valid solution, any of them will be accepted.
16+
17+
If no valid solution exists, return an empty 2-D array.
718

819
**Example 1:**
920

1021
```
11-
Input: a = "11", b = "1"
12-
Output: "100"
22+
Input: upper = 2, lower = 1, colsum = [1,1,1]
23+
Output: [[1,1,0],[0,0,1]]
24+
Explanation: [[1,0,1],[0,1,0]], and [[0,1,1],[1,0,0]] are also correct answers.
1325
```
1426

15-
## 题意
16-
> ...
27+
**Example 2:**
1728

18-
## 题解
19-
20-
### 思路1
21-
> ...
22-
Reconstruct a 2-Row Binary Matrix
23-
```go
2429
```
30+
Input: upper = 2, lower = 3, colsum = [2,2,1,1]
31+
Output: []
32+
```
33+
34+
**Example 3:**
2535

36+
```
37+
Input: upper = 5, lower = 5, colsum = [2,1,2,0,1,0,1,2,0,1]
38+
Output: [[1,1,1,0,1,0,0,1,0,0],[1,0,1,0,0,0,1,1,0,1]]
39+
```
2640

2741
## 结语
2842

Lines changed: 44 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,47 @@
11
package Solution
22

3-
func Solution(x bool) bool {
4-
return x
3+
func Solution(upper int, lower int, colsum []int) [][]int {
4+
res := make([][]int, 2)
5+
l := len(colsum)
6+
sum := 0
7+
for i := range l {
8+
sum += colsum[i]
9+
}
10+
if sum != upper+lower {
11+
return nil
12+
}
13+
14+
for i := range 2 {
15+
res[i] = make([]int, l)
16+
}
17+
18+
for i := range l {
19+
if colsum[i] == 2 {
20+
if upper == 0 || lower == 0 {
21+
return nil
22+
}
23+
res[0][i] = 1
24+
res[1][i] = 1
25+
upper--
26+
lower--
27+
}
28+
}
29+
30+
for i := range l {
31+
if colsum[i] == 1 {
32+
if upper > 0 {
33+
res[0][i] = 1
34+
upper--
35+
continue
36+
}
37+
if lower > 0 {
38+
res[1][i] = 1
39+
lower--
40+
continue
41+
}
42+
return nil
43+
}
44+
}
45+
46+
return res
547
}

leetcode/1201-1300/1253.Reconstruct-a-2-Row-Binary-Matrix/Solution_test.go

Lines changed: 12 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -9,31 +9,32 @@ import (
99
func TestSolution(t *testing.T) {
1010
// 测试用例
1111
cases := []struct {
12-
name string
13-
inputs bool
14-
expect bool
12+
name string
13+
upper, lower int
14+
colsum []int
15+
expect [][]int
1516
}{
16-
{"TestCase", true, true},
17-
{"TestCase", true, true},
18-
{"TestCase", false, false},
17+
{"TestCase1", 2, 1, []int{1, 1, 1}, [][]int{{1, 1, 0}, {0, 0, 1}}},
18+
{"TestCase2", 2, 3, []int{2, 2, 1, 1}, nil},
19+
{"TestCase3", 5, 5, []int{2, 1, 2, 0, 1, 0, 1, 2, 0, 1}, [][]int{{1, 1, 1, 0, 1, 0, 0, 1, 0, 0}, {1, 0, 1, 0, 0, 0, 1, 1, 0, 1}}},
1920
}
2021

2122
// 开始测试
2223
for i, c := range cases {
2324
t.Run(c.name+" "+strconv.Itoa(i), func(t *testing.T) {
24-
got := Solution(c.inputs)
25+
got := Solution(c.upper, c.lower, c.colsum)
2526
if !reflect.DeepEqual(got, c.expect) {
26-
t.Fatalf("expected: %v, but got: %v, with inputs: %v",
27-
c.expect, got, c.inputs)
27+
t.Fatalf("expected: %v, but got: %v, with inputs: %v %v %v",
28+
c.expect, got, c.upper, c.lower, c.colsum)
2829
}
2930
})
3031
}
3132
}
3233

33-
// 压力测试
34+
// 压力测试
3435
func BenchmarkSolution(b *testing.B) {
3536
}
3637

37-
// 使用案列
38+
// 使用案列
3839
func ExampleSolution() {
3940
}

0 commit comments

Comments
 (0)