From 06fe0f41a9cc935839161909d925c5c8b0b4885e Mon Sep 17 00:00:00 2001 From: 0xff-dev Date: Mon, 30 Dec 2024 09:17:09 +0800 Subject: [PATCH] Add solution and test-cases for problem 1253 --- .../README.md | 40 ++++++++++------ .../Solution.go | 46 ++++++++++++++++++- .../Solution_test.go | 23 +++++----- 3 files changed, 83 insertions(+), 26 deletions(-) diff --git a/leetcode/1201-1300/1253.Reconstruct-a-2-Row-Binary-Matrix/README.md b/leetcode/1201-1300/1253.Reconstruct-a-2-Row-Binary-Matrix/README.md index 9a73c986c..baf3cedfd 100644 --- a/leetcode/1201-1300/1253.Reconstruct-a-2-Row-Binary-Matrix/README.md +++ b/leetcode/1201-1300/1253.Reconstruct-a-2-Row-Binary-Matrix/README.md @@ -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]] +``` ## 结语 diff --git a/leetcode/1201-1300/1253.Reconstruct-a-2-Row-Binary-Matrix/Solution.go b/leetcode/1201-1300/1253.Reconstruct-a-2-Row-Binary-Matrix/Solution.go index d115ccf5e..e37b69440 100644 --- a/leetcode/1201-1300/1253.Reconstruct-a-2-Row-Binary-Matrix/Solution.go +++ b/leetcode/1201-1300/1253.Reconstruct-a-2-Row-Binary-Matrix/Solution.go @@ -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 } diff --git a/leetcode/1201-1300/1253.Reconstruct-a-2-Row-Binary-Matrix/Solution_test.go b/leetcode/1201-1300/1253.Reconstruct-a-2-Row-Binary-Matrix/Solution_test.go index 14ff50eb4..ffa199076 100644 --- a/leetcode/1201-1300/1253.Reconstruct-a-2-Row-Binary-Matrix/Solution_test.go +++ b/leetcode/1201-1300/1253.Reconstruct-a-2-Row-Binary-Matrix/Solution_test.go @@ -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() { }