Skip to content

Commit 75ba6a2

Browse files
committed
Add solution and test-cases for problem 2392
1 parent 9f83439 commit 75ba6a2

File tree

4 files changed

+112
-28
lines changed

4 files changed

+112
-28
lines changed

leetcode/2301-2400/2392.Build-a-Matrix-With-Conditions/README.md

Lines changed: 34 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -1,28 +1,47 @@
11
# [2392.Build a Matrix With Conditions][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 a **positive** integer `k`. You are also given:
75

8-
**Example 1:**
6+
- a 2D integer array `rowConditions` of size n where `rowConditions[i] = [abovei, belowi]`, and
7+
- a 2D integer array `colConditions` of size m where `colConditions[i] = [lefti, righti]`.
98

10-
```
11-
Input: a = "11", b = "1"
12-
Output: "100"
13-
```
9+
The two arrays contain integers from `1` to `k`.
10+
11+
You have to build a `k x k` matrix that contains each of the numbers from `1` to `k` **exactly once**. The remaining cells should have the value `0`.
12+
13+
The matrix should also satisfy the following conditions:
14+
15+
- The number `abovei` should appear in a **row** that is strictly **above** the row at which the number `belowi` appears for all `i` from `0` to `n - 1`.
16+
- The number `lefti` should appear in a **column** that is strictly **left** of the column at which the number `righti` appears for all `i` from `0` to `m - 1`.
1417

15-
## 题意
16-
> ...
18+
Return **any** matrix that satisfies the conditions. If no answer exists, return an empty matrix.
1719

18-
## 题解
20+
**Example 1:**
1921

20-
### 思路1
21-
> ...
22-
Build a Matrix With Conditions
23-
```go
22+
![1](gridosdrawio.png)
23+
24+
```
25+
Input: k = 3, rowConditions = [[1,2],[3,2]], colConditions = [[2,1],[3,2]]
26+
Output: [[3,0,0],[0,0,1],[0,2,0]]
27+
Explanation: The diagram above shows a valid example of a matrix that satisfies all the conditions.
28+
The row conditions are the following:
29+
- Number 1 is in row 1, and number 2 is in row 2, so 1 is above 2 in the matrix.
30+
- Number 3 is in row 0, and number 2 is in row 2, so 3 is above 2 in the matrix.
31+
The column conditions are the following:
32+
- Number 2 is in column 1, and number 1 is in column 2, so 2 is left of 1 in the matrix.
33+
- Number 3 is in column 0, and number 2 is in column 1, so 3 is left of 2 in the matrix.
34+
Note that there may be multiple correct answers.
2435
```
2536

37+
**Example 2:**
38+
39+
```
40+
Input: k = 3, rowConditions = [[1,2],[2,3],[3,1],[2,3]], colConditions = [[2,1]]
41+
Output: []
42+
Explanation: From the first two conditions, 3 has to be below 1 but the third conditions needs 3 to be above 1 to be satisfied.
43+
No matrix can satisfy all the conditions, so we return the empty matrix.
44+
```
2645

2746
## 结语
2847

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

3-
func Solution(x bool) bool {
4-
return x
3+
func sortConditions(k int, conditions [][]int) (map[int]int, bool) {
4+
children := make(map[int][]int)
5+
in := make(map[int]int)
6+
for _, c := range conditions {
7+
if _, ok := children[c[0]]; !ok {
8+
children[c[0]] = make([]int, 0)
9+
}
10+
children[c[0]] = append(children[c[0]], c[1])
11+
in[c[1]]++
12+
}
13+
index := 0
14+
pos := make(map[int]int)
15+
have := make([]int, 0)
16+
left := k
17+
for i := 1; i <= k; i++ {
18+
if _, ok := in[i]; !ok {
19+
pos[i] = index
20+
have = append(have, i)
21+
index++
22+
left--
23+
}
24+
}
25+
for left > 0 {
26+
next := make([]int, 0)
27+
for _, cur := range have {
28+
for _, rel := range children[cur] {
29+
in[rel]--
30+
if in[rel] == 0 {
31+
next = append(next, rel)
32+
left--
33+
pos[rel] = index
34+
index++
35+
}
36+
}
37+
}
38+
if len(next) == 0 && left > 0 {
39+
return nil, false
40+
}
41+
have = next
42+
}
43+
return pos, true
44+
}
45+
46+
func Solution(k int, rowConditions [][]int, colConditions [][]int) [][]int {
47+
ans := make([][]int, k)
48+
for i := 0; i < k; i++ {
49+
ans[i] = make([]int, k)
50+
}
51+
rowPos, ok := sortConditions(k, rowConditions)
52+
if !ok {
53+
return nil
54+
}
55+
56+
colPos, ok := sortConditions(k, colConditions)
57+
if !ok {
58+
return nil
59+
}
60+
61+
for i := 1; i <= k; i++ {
62+
ans[rowPos[i]][colPos[i]] = i
63+
}
64+
65+
return ans
566
}

leetcode/2301-2400/2392.Build-a-Matrix-With-Conditions/Solution_test.go

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

2125
// 开始测试
2226
for i, c := range cases {
2327
t.Run(c.name+" "+strconv.Itoa(i), func(t *testing.T) {
24-
got := Solution(c.inputs)
28+
got := Solution(c.k, c.rowConditions, c.colConditions)
2529
if !reflect.DeepEqual(got, c.expect) {
26-
t.Fatalf("expected: %v, but got: %v, with inputs: %v",
27-
c.expect, got, c.inputs)
30+
t.Fatalf("expected: %v, but got: %v, with inputs: %v %v %v",
31+
c.expect, got, c.k, c.rowConditions, c.colConditions)
2832
}
2933
})
3034
}
3135
}
3236

33-
// 压力测试
37+
// 压力测试
3438
func BenchmarkSolution(b *testing.B) {
3539
}
3640

37-
// 使用案列
41+
// 使用案列
3842
func ExampleSolution() {
3943
}
Loading

0 commit comments

Comments
 (0)