From 31cc63498d69e77335f1a3b6e7b1fdb1ec577b0a Mon Sep 17 00:00:00 2001 From: 0xff-dev Date: Sat, 20 Jul 2024 12:37:51 +0800 Subject: [PATCH] Add solution and test-cases for problem 1605 --- .../README.md | 36 +++++++++++-------- .../Solution.go | 20 +++++++++-- .../Solution_test.go | 19 +++++----- 3 files changed, 49 insertions(+), 26 deletions(-) diff --git a/leetcode/1601-1700/1605.Find-Valid-Matrix-Given-Row-and-Column-Sums/README.md b/leetcode/1601-1700/1605.Find-Valid-Matrix-Given-Row-and-Column-Sums/README.md index 3065060dd..49726a32a 100755 --- a/leetcode/1601-1700/1605.Find-Valid-Matrix-Given-Row-and-Column-Sums/README.md +++ b/leetcode/1601-1700/1605.Find-Valid-Matrix-Given-Row-and-Column-Sums/README.md @@ -1,28 +1,36 @@ # [1605.Find Valid Matrix Given Row and Column Sums][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 +You are given two arrays `rowSum` and `colSum` of non-negative integers where `rowSum[i]` is the sum of the elements in the `ith` row and `colSum[j]` is the sum of the elements of the `jth` column of a 2D matrix. In other words, you do not know the elements of the matrix, but you do know the sums of each row and column. + +Find any matrix of **non-negative** integers of size `rowSum.length x colSum.length` that satisfies the `rowSum` and `colSum` requirements. + +Return a 2D array representing **any** matrix that fulfills the requirements. It's guaranteed that **at least one** matrix that fulfills the requirements exists. **Example 1:** ``` -Input: a = "11", b = "1" -Output: "100" +Input: rowSum = [3,8], colSum = [4,7] +Output: [[3,0], + [1,7]] +Explanation: +0th row: 3 + 0 = 3 == rowSum[0] +1st row: 1 + 7 = 8 == rowSum[1] +0th column: 3 + 1 = 4 == colSum[0] +1st column: 0 + 7 = 7 == colSum[1] +The row and column sums match, and all matrix elements are non-negative. +Another possible matrix is: [[1,2], + [3,5]] ``` -## 题意 -> ... +**Example 2:** -## 题解 - -### 思路1 -> ... -Find Valid Matrix Given Row and Column Sums -```go ``` - +Input: rowSum = [5,7,10], colSum = [8,6,8] +Output: [[0,5,0], + [6,1,0], + [2,0,8]] +``` ## 结语 diff --git a/leetcode/1601-1700/1605.Find-Valid-Matrix-Given-Row-and-Column-Sums/Solution.go b/leetcode/1601-1700/1605.Find-Valid-Matrix-Given-Row-and-Column-Sums/Solution.go index d115ccf5e..8d30b4578 100644 --- a/leetcode/1601-1700/1605.Find-Valid-Matrix-Given-Row-and-Column-Sums/Solution.go +++ b/leetcode/1601-1700/1605.Find-Valid-Matrix-Given-Row-and-Column-Sums/Solution.go @@ -1,5 +1,21 @@ package Solution -func Solution(x bool) bool { - return x +func Solution(rowSum []int, colSum []int) [][]int { + N := len(rowSum) + M := len(colSum) + + currRowSum := make([]int, N) + currColSum := make([]int, M) + + origMatrix := make([][]int, N) + for i := 0; i < N; i++ { + origMatrix[i] = make([]int, M) + for j := 0; j < M; j++ { + origMatrix[i][j] = min(rowSum[i]-currRowSum[i], colSum[j]-currColSum[j]) + + currRowSum[i] += origMatrix[i][j] + currColSum[j] += origMatrix[i][j] + } + } + return origMatrix } diff --git a/leetcode/1601-1700/1605.Find-Valid-Matrix-Given-Row-and-Column-Sums/Solution_test.go b/leetcode/1601-1700/1605.Find-Valid-Matrix-Given-Row-and-Column-Sums/Solution_test.go index 14ff50eb4..7552afd0d 100644 --- a/leetcode/1601-1700/1605.Find-Valid-Matrix-Given-Row-and-Column-Sums/Solution_test.go +++ b/leetcode/1601-1700/1605.Find-Valid-Matrix-Given-Row-and-Column-Sums/Solution_test.go @@ -10,30 +10,29 @@ func TestSolution(t *testing.T) { // 测试用例 cases := []struct { name string - inputs bool - expect bool + a, b []int + expect [][]int }{ - {"TestCase", true, true}, - {"TestCase", true, true}, - {"TestCase", false, false}, + {"TestCase1", []int{3, 8}, []int{4, 7}, [][]int{{3, 0}, {1, 7}}}, + {"TestCase2", []int{5, 7, 10}, []int{8, 6, 8}, [][]int{{5, 0, 0}, {3, 4, 0}, {0, 2, 8}}}, } // 开始测试 for i, c := range cases { t.Run(c.name+" "+strconv.Itoa(i), func(t *testing.T) { - got := Solution(c.inputs) + got := Solution(c.a, c.b) 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", + c.expect, got, c.a, c.b) } }) } } -// 压力测试 +// 压力测试 func BenchmarkSolution(b *testing.B) { } -// 使用案列 +// 使用案列 func ExampleSolution() { }