Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add solution and test-cases for problem 1861 #1041

Merged
merged 1 commit into from
Dec 24, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Binary file added leetcode/1801-1900/1861.Rotating-the-Box/1.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added leetcode/1801-1900/1861.Rotating-the-Box/2.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added leetcode/1801-1900/1861.Rotating-the-Box/3.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
55 changes: 42 additions & 13 deletions leetcode/1801-1900/1861.Rotating-the-Box/README.md
Original file line number Diff line number Diff line change
@@ -1,28 +1,57 @@
# [1861.Rotating the Box][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 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:

- A stone `'#'`
- A stationary obstacle `'*'`
- Empty `'.'`

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.

It is **guaranteed** that each stone in `box` rests on an obstacle, another stone, or the bottom of the box.

Return an `n x m` matrix representing the box after the rotation described above.

**Example 1:**

**Example 1:**
![1](./1.png)

```
Input: a = "11", b = "1"
Output: "100"
Input: box = [["#",".","#"]]
Output: [["."],
["#"],
["#"]]
```

## 题意
> ...
**Example 2:**

## 题解
![2](./2.png)

### 思路1
> ...
Rotating the Box
```go
```
Input: box = [["#",".","*","."],
["#","#","*","."]]
Output: [["#","."],
["#","#"],
["*","*"],
[".","."]]
```

**Example 3:**

![3](./3.png)

```
Input: box = [["#","#","*",".","*","."],
["#","#","#","*",".","."],
["#","#","#",".","#","."]]
Output: [[".","#","#"],
[".","#","#"],
["#","#","*"],
["#","*","."],
["#",".","*"],
["#",".","."]]
```

## 结语

Expand Down
28 changes: 26 additions & 2 deletions leetcode/1801-1900/1861.Rotating-the-Box/Solution.go
Original file line number Diff line number Diff line change
@@ -1,5 +1,29 @@
package Solution

func Solution(x bool) bool {
return x
func Solution(box [][]byte) [][]byte {
rows, cols := len(box), len(box[0])
for i := 0; i < rows; i++ {
for j := cols - 2; j >= 0; j-- {
if box[i][j] == '.' || box[i][j] == '*' {
continue
}
next := j + 1
for ; next < cols; next++ {
if box[i][next] == '#' || box[i][next] == '*' {
break
}
}
box[i][j] = '.'
box[i][next-1] = '#'
}
}
res := make([][]byte, cols)
for i := range cols {
res[i] = make([]byte, rows)
for j := 0; j < rows; j++ {
res[i][j] = box[rows-1-j][i]
}
}

return res
}
33 changes: 26 additions & 7 deletions leetcode/1801-1900/1861.Rotating-the-Box/Solution_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,12 +10,31 @@ func TestSolution(t *testing.T) {
// 测试用例
cases := []struct {
name string
inputs bool
expect bool
inputs [][]byte
expect [][]byte
}{
{"TestCase", true, true},
{"TestCase", true, true},
{"TestCase", false, false},
{"TestCase1", [][]byte{{'#', '.', '#'}}, [][]byte{{'.'}, {'#'}, {'#'}}},
{"TestCase2", [][]byte{
{'#', '.', '*', '.'},
{'#', '#', '*', '.'},
}, [][]byte{
{'#', '.'},
{'#', '#'},
{'*', '*'},
{'.', '.'},
}},
{"TestCase3", [][]byte{
{'#', '#', '*', '.', '*', '.'},
{'#', '#', '#', '*', '.', '.'},
{'#', '#', '#', '.', '#', '.'},
}, [][]byte{
{'.', '#', '#'},
{'.', '#', '#'},
{'#', '#', '*'},
{'#', '*', '.'},
{'#', '.', '*'},
{'#', '.', '.'},
}},
}

// 开始测试
Expand All @@ -30,10 +49,10 @@ func TestSolution(t *testing.T) {
}
}

// 压力测试
// 压力测试
func BenchmarkSolution(b *testing.B) {
}

// 使用案列
// 使用案列
func ExampleSolution() {
}
Loading