Skip to content

Commit 634f12f

Browse files
authored
Merge pull request #1041 from 0xff-dev/1861
Add solution and test-cases for problem 1861
2 parents e37ea1b + d280aa4 commit 634f12f

File tree

6 files changed

+94
-22
lines changed

6 files changed

+94
-22
lines changed
18.9 KB
Loading
27.9 KB
Loading
70.3 KB
Loading

leetcode/1801-1900/1861.Rotating-the-Box/README.md

Lines changed: 42 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -1,28 +1,57 @@
11
# [1861.Rotating the Box][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 `m x n` matrix of characters `box` representing a side-view of a box. Each cell of the box is one of the following:
5+
6+
- A stone `'#'`
7+
- A stationary obstacle `'*'`
8+
- Empty `'.'`
9+
10+
The box is rotated **90 degrees clockwise**, causing some of the stones to fall due to gravity. Each stone falls down until it lands on an obstacle, another stone, or the bottom of the box. Gravity **does not** affect the obstacles' positions, and the inertia from the box's rotation **does not** affect the stones' horizontal positions.
11+
12+
It is **guaranteed** that each stone in `box` rests on an obstacle, another stone, or the bottom of the box.
13+
14+
Return an `n x m` matrix representing the box after the rotation described above.
15+
16+
**Example 1:**
717

8-
**Example 1:**
18+
![1](./1.png)
919

1020
```
11-
Input: a = "11", b = "1"
12-
Output: "100"
21+
Input: box = [["#",".","#"]]
22+
Output: [["."],
23+
["#"],
24+
["#"]]
1325
```
1426

15-
## 题意
16-
> ...
27+
**Example 2:**
1728

18-
## 题解
29+
![2](./2.png)
1930

20-
### 思路1
21-
> ...
22-
Rotating the Box
23-
```go
2431
```
32+
Input: box = [["#",".","*","."],
33+
["#","#","*","."]]
34+
Output: [["#","."],
35+
["#","#"],
36+
["*","*"],
37+
[".","."]]
38+
```
39+
40+
**Example 3:**
41+
42+
![3](./3.png)
2543

44+
```
45+
Input: box = [["#","#","*",".","*","."],
46+
["#","#","#","*",".","."],
47+
["#","#","#",".","#","."]]
48+
Output: [[".","#","#"],
49+
[".","#","#"],
50+
["#","#","*"],
51+
["#","*","."],
52+
["#",".","*"],
53+
["#",".","."]]
54+
```
2655

2756
## 结语
2857

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

3-
func Solution(x bool) bool {
4-
return x
3+
func Solution(box [][]byte) [][]byte {
4+
rows, cols := len(box), len(box[0])
5+
for i := 0; i < rows; i++ {
6+
for j := cols - 2; j >= 0; j-- {
7+
if box[i][j] == '.' || box[i][j] == '*' {
8+
continue
9+
}
10+
next := j + 1
11+
for ; next < cols; next++ {
12+
if box[i][next] == '#' || box[i][next] == '*' {
13+
break
14+
}
15+
}
16+
box[i][j] = '.'
17+
box[i][next-1] = '#'
18+
}
19+
}
20+
res := make([][]byte, cols)
21+
for i := range cols {
22+
res[i] = make([]byte, rows)
23+
for j := 0; j < rows; j++ {
24+
res[i][j] = box[rows-1-j][i]
25+
}
26+
}
27+
28+
return res
529
}

leetcode/1801-1900/1861.Rotating-the-Box/Solution_test.go

Lines changed: 26 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -10,12 +10,31 @@ func TestSolution(t *testing.T) {
1010
// 测试用例
1111
cases := []struct {
1212
name string
13-
inputs bool
14-
expect bool
13+
inputs [][]byte
14+
expect [][]byte
1515
}{
16-
{"TestCase", true, true},
17-
{"TestCase", true, true},
18-
{"TestCase", false, false},
16+
{"TestCase1", [][]byte{{'#', '.', '#'}}, [][]byte{{'.'}, {'#'}, {'#'}}},
17+
{"TestCase2", [][]byte{
18+
{'#', '.', '*', '.'},
19+
{'#', '#', '*', '.'},
20+
}, [][]byte{
21+
{'#', '.'},
22+
{'#', '#'},
23+
{'*', '*'},
24+
{'.', '.'},
25+
}},
26+
{"TestCase3", [][]byte{
27+
{'#', '#', '*', '.', '*', '.'},
28+
{'#', '#', '#', '*', '.', '.'},
29+
{'#', '#', '#', '.', '#', '.'},
30+
}, [][]byte{
31+
{'.', '#', '#'},
32+
{'.', '#', '#'},
33+
{'#', '#', '*'},
34+
{'#', '*', '.'},
35+
{'#', '.', '*'},
36+
{'#', '.', '.'},
37+
}},
1938
}
2039

2140
// 开始测试
@@ -30,10 +49,10 @@ func TestSolution(t *testing.T) {
3049
}
3150
}
3251

33-
// 压力测试
52+
// 压力测试
3453
func BenchmarkSolution(b *testing.B) {
3554
}
3655

37-
// 使用案列
56+
// 使用案列
3857
func ExampleSolution() {
3958
}

0 commit comments

Comments
 (0)