Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
40 changes: 27 additions & 13 deletions leetcode/1201-1300/1253.Reconstruct-a-2-Row-Binary-Matrix/README.md
Original file line number Diff line number Diff line change
@@ -1,28 +1,42 @@
# [1253.Reconstruct a 2-Row Binary Matrix][title]

> [!WARNING|style:flat]
> This question is temporarily unanswered if you have good ideas. Welcome to [Create Pull Request PR](https://github.com/kylesliu/awesome-golang-algorithm)

## Description
Given the following details of a matrix with `n` columns and `2` rows :

- The matrix is a binary matrix, which means each element in the matrix can be `0` or `1`.
- The sum of elements of the 0-th(upper) row is given as `upper`.
- The sum of elements of the 1-st(lower) row is given as `lower`.
- 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`.

Your task is to reconstruct the matrix with `upper`, `lower` and `colsum`.

Return it as a 2-D integer array.

If there are more than one valid solution, any of them will be accepted.

If no valid solution exists, return an empty 2-D array.

**Example 1:**

```
Input: a = "11", b = "1"
Output: "100"
Input: upper = 2, lower = 1, colsum = [1,1,1]
Output: [[1,1,0],[0,0,1]]
Explanation: [[1,0,1],[0,1,0]], and [[0,1,1],[1,0,0]] are also correct answers.
```

## 题意
> ...
**Example 2:**

## 题解

### 思路1
> ...
Reconstruct a 2-Row Binary Matrix
```go
```
Input: upper = 2, lower = 3, colsum = [2,2,1,1]
Output: []
```

**Example 3:**

```
Input: upper = 5, lower = 5, colsum = [2,1,2,0,1,0,1,2,0,1]
Output: [[1,1,1,0,1,0,0,1,0,0],[1,0,1,0,0,0,1,1,0,1]]
```

## 结语

Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,47 @@
package Solution

func Solution(x bool) bool {
return x
func Solution(upper int, lower int, colsum []int) [][]int {
res := make([][]int, 2)
l := len(colsum)
sum := 0
for i := range l {
sum += colsum[i]
}
if sum != upper+lower {
return nil
}

for i := range 2 {
res[i] = make([]int, l)
}

for i := range l {
if colsum[i] == 2 {
if upper == 0 || lower == 0 {
return nil
}
res[0][i] = 1
res[1][i] = 1
upper--
lower--
}
}

for i := range l {
if colsum[i] == 1 {
if upper > 0 {
res[0][i] = 1
upper--
continue
}
if lower > 0 {
res[1][i] = 1
lower--
continue
}
return nil
}
}

return res
}
Original file line number Diff line number Diff line change
Expand Up @@ -9,31 +9,32 @@ import (
func TestSolution(t *testing.T) {
// 测试用例
cases := []struct {
name string
inputs bool
expect bool
name string
upper, lower int
colsum []int
expect [][]int
}{
{"TestCase", true, true},
{"TestCase", true, true},
{"TestCase", false, false},
{"TestCase1", 2, 1, []int{1, 1, 1}, [][]int{{1, 1, 0}, {0, 0, 1}}},
{"TestCase2", 2, 3, []int{2, 2, 1, 1}, nil},
{"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}}},
}

// 开始测试
for i, c := range cases {
t.Run(c.name+" "+strconv.Itoa(i), func(t *testing.T) {
got := Solution(c.inputs)
got := Solution(c.upper, c.lower, c.colsum)
if !reflect.DeepEqual(got, c.expect) {
t.Fatalf("expected: %v, but got: %v, with inputs: %v",
c.expect, got, c.inputs)
t.Fatalf("expected: %v, but got: %v, with inputs: %v %v %v",
c.expect, got, c.upper, c.lower, c.colsum)
}
})
}
}

// 压力测试
// 压力测试
func BenchmarkSolution(b *testing.B) {
}

// 使用案列
// 使用案列
func ExampleSolution() {
}
Loading