Skip to content

Commit 92f5a58

Browse files
authored
Merge pull request #936 from 0xff-dev/1605
Add solution and test-cases for problem 1605
2 parents fcb2eb0 + 31cc634 commit 92f5a58

File tree

3 files changed

+49
-26
lines changed

3 files changed

+49
-26
lines changed

leetcode/1601-1700/1605.Find-Valid-Matrix-Given-Row-and-Column-Sums/README.md

Lines changed: 22 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -1,28 +1,36 @@
11
# [1605.Find Valid Matrix Given Row and Column Sums][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+
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.
5+
6+
Find any matrix of **non-negative** integers of size `rowSum.length x colSum.length` that satisfies the `rowSum` and `colSum` requirements.
7+
8+
Return a 2D array representing **any** matrix that fulfills the requirements. It's guaranteed that **at least one** matrix that fulfills the requirements exists.
79

810
**Example 1:**
911

1012
```
11-
Input: a = "11", b = "1"
12-
Output: "100"
13+
Input: rowSum = [3,8], colSum = [4,7]
14+
Output: [[3,0],
15+
[1,7]]
16+
Explanation:
17+
0th row: 3 + 0 = 3 == rowSum[0]
18+
1st row: 1 + 7 = 8 == rowSum[1]
19+
0th column: 3 + 1 = 4 == colSum[0]
20+
1st column: 0 + 7 = 7 == colSum[1]
21+
The row and column sums match, and all matrix elements are non-negative.
22+
Another possible matrix is: [[1,2],
23+
[3,5]]
1324
```
1425

15-
## 题意
16-
> ...
26+
**Example 2:**
1727

18-
## 题解
19-
20-
### 思路1
21-
> ...
22-
Find Valid Matrix Given Row and Column Sums
23-
```go
2428
```
25-
29+
Input: rowSum = [5,7,10], colSum = [8,6,8]
30+
Output: [[0,5,0],
31+
[6,1,0],
32+
[2,0,8]]
33+
```
2634

2735
## 结语
2836

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

3-
func Solution(x bool) bool {
4-
return x
3+
func Solution(rowSum []int, colSum []int) [][]int {
4+
N := len(rowSum)
5+
M := len(colSum)
6+
7+
currRowSum := make([]int, N)
8+
currColSum := make([]int, M)
9+
10+
origMatrix := make([][]int, N)
11+
for i := 0; i < N; i++ {
12+
origMatrix[i] = make([]int, M)
13+
for j := 0; j < M; j++ {
14+
origMatrix[i][j] = min(rowSum[i]-currRowSum[i], colSum[j]-currColSum[j])
15+
16+
currRowSum[i] += origMatrix[i][j]
17+
currColSum[j] += origMatrix[i][j]
18+
}
19+
}
20+
return origMatrix
521
}

leetcode/1601-1700/1605.Find-Valid-Matrix-Given-Row-and-Column-Sums/Solution_test.go

Lines changed: 9 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -10,30 +10,29 @@ func TestSolution(t *testing.T) {
1010
// 测试用例
1111
cases := []struct {
1212
name string
13-
inputs bool
14-
expect bool
13+
a, b []int
14+
expect [][]int
1515
}{
16-
{"TestCase", true, true},
17-
{"TestCase", true, true},
18-
{"TestCase", false, false},
16+
{"TestCase1", []int{3, 8}, []int{4, 7}, [][]int{{3, 0}, {1, 7}}},
17+
{"TestCase2", []int{5, 7, 10}, []int{8, 6, 8}, [][]int{{5, 0, 0}, {3, 4, 0}, {0, 2, 8}}},
1918
}
2019

2120
// 开始测试
2221
for i, c := range cases {
2322
t.Run(c.name+" "+strconv.Itoa(i), func(t *testing.T) {
24-
got := Solution(c.inputs)
23+
got := Solution(c.a, c.b)
2524
if !reflect.DeepEqual(got, c.expect) {
26-
t.Fatalf("expected: %v, but got: %v, with inputs: %v",
27-
c.expect, got, c.inputs)
25+
t.Fatalf("expected: %v, but got: %v, with inputs: %v %v",
26+
c.expect, got, c.a, c.b)
2827
}
2928
})
3029
}
3130
}
3231

33-
// 压力测试
32+
// 压力测试
3433
func BenchmarkSolution(b *testing.B) {
3534
}
3635

37-
// 使用案列
36+
// 使用案列
3837
func ExampleSolution() {
3938
}

0 commit comments

Comments
 (0)