Skip to content

Commit ccce13f

Browse files
committed
Add solution and test-cases for problem 3446
1 parent df4288a commit ccce13f

File tree

5 files changed

+79
-22
lines changed

5 files changed

+79
-22
lines changed
26.9 KB
Loading
15.8 KB
Loading

leetcode/3401-3500/3446.Sort-Matrix-by-Diagonals/README.md

Lines changed: 42 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -1,28 +1,57 @@
11
# [3446.Sort Matrix by Diagonals][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 an `n x n` square matrix of integers `grid`. Return the matrix such that:
5+
6+
- The diagonals in the **bottom-left triangle** (including the middle diagonal) are sorted in **non-increasing order**.
7+
- The diagonals in the **top-right triangle** are sorted in **non-decreasing order**.
8+
9+
**Example 1:**
10+
11+
![1](./1.png)
12+
13+
```
14+
Input: grid = [[1,7,3],[9,8,2],[4,5,6]]
15+
16+
Output: [[8,2,3],[9,6,7],[4,5,1]]
17+
18+
Explanation:
19+
20+
The diagonals with a black arrow (bottom-left triangle) should be sorted in non-increasing order:
721
8-
**Example 1:**
22+
[1, 8, 6] becomes [8, 6, 1].
23+
[9, 5] and [4] remain unchanged.
24+
The diagonals with a blue arrow (top-right triangle) should be sorted in non-decreasing order:
925
26+
[7, 2] becomes [2, 7].
27+
[3] remains unchanged.
1028
```
11-
Input: a = "11", b = "1"
12-
Output: "100"
29+
30+
**Example 2:**
31+
32+
![2](./2.png)
33+
1334
```
35+
Input: grid = [[0,1],[1,2]]
1436
15-
## 题意
16-
> ...
37+
Output: [[2,1],[1,0]]
1738
18-
## 题解
39+
Explanation:
1940
20-
### 思路1
21-
> ...
22-
Sort Matrix by Diagonals
23-
```go
41+
The diagonals with a black arrow must be non-increasing, so [0, 2] is changed to [2, 0]. The other diagonals are already in the correct order.
2442
```
2543

44+
**Example 3:**
45+
46+
```
47+
Input: grid = [[1]]
48+
49+
Output: [[1]]
50+
51+
Explanation:
52+
53+
Diagonals with exactly one element are already in order, so no changes are needed.
54+
```
2655

2756
## 结语
2857

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

3-
func Solution(x bool) bool {
4-
return x
3+
import "sort"
4+
5+
func Solution(grid [][]int) [][]int {
6+
n := len(grid)
7+
for i := 0; i < n; i++ {
8+
// j = 0
9+
have := []int{}
10+
for r, j := i, 0; r < n; r, j = r+1, j+1 {
11+
have = append(have, grid[r][j])
12+
}
13+
sort.Slice(have, func(i, j int) bool {
14+
return have[i] > have[j]
15+
})
16+
index := 0
17+
for r, j := i, 0; r < n; r, j, index = r+1, j+1, index+1 {
18+
grid[r][j] = have[index]
19+
}
20+
}
21+
for j := 1; j < n; j++ {
22+
// i = 1
23+
have := []int{}
24+
for i, r := 0, j; r < n; i, r = i+1, r+1 {
25+
have = append(have, grid[i][r])
26+
}
27+
sort.Ints(have)
28+
for i, r, index := 0, j, 0; r < n; i, r, index = i+1, r+1, index+1 {
29+
grid[i][r] = have[index]
30+
}
31+
}
32+
return grid
533
}

leetcode/3401-3500/3446.Sort-Matrix-by-Diagonals/Solution_test.go

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

2121
// 开始测试
@@ -30,10 +30,10 @@ func TestSolution(t *testing.T) {
3030
}
3131
}
3232

33-
//压力测试
33+
// 压力测试
3434
func BenchmarkSolution(b *testing.B) {
3535
}
3636

37-
//使用案列
37+
// 使用案列
3838
func ExampleSolution() {
3939
}

0 commit comments

Comments
 (0)