Skip to content
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
106 changes: 90 additions & 16 deletions solution/1800-1899/1861.Rotating the Box/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -97,6 +97,8 @@ tags:

我们先将矩阵顺时针旋转 90 度,然后模拟每一列石头的下落过程。

具体地,我们使用一个队列 $q$ 来存储当前列中空位置的行号。遍历每一列,我们从下往上扫描,如果遇到一个石头,我们就将它掉落到 $q$ 中第一个空位置,并将这个空位置从 $q$ 中移除,由于当前位置变成了空位置,我们就将它的行号加入 $q$ 中;如果遇到一个障碍物,我们就清空 $q$,因为石头无法穿过障碍物;如果遇到一个空位置,我们就将它的行号加入 $q$ 中。

时间复杂度 $O(m \times n)$,空间复杂度 $O(m \times n)$。其中 $m$ 和 $n$ 分别是矩阵的行数和列数。

<!-- tabs:start -->
Expand All @@ -105,22 +107,22 @@ tags:

```python
class Solution:
def rotateTheBox(self, box: List[List[str]]) -> List[List[str]]:
m, n = len(box), len(box[0])
def rotateTheBox(self, boxGrid: List[List[str]]) -> List[List[str]]:
m, n = len(boxGrid), len(boxGrid[0])
ans = [[None] * m for _ in range(n)]
for i in range(m):
for j in range(n):
ans[j][m - i - 1] = box[i][j]
ans[j][m - i - 1] = boxGrid[i][j]
for j in range(m):
q = deque()
for i in range(n - 1, -1, -1):
if ans[i][j] == '*':
if ans[i][j] == "*":
q.clear()
elif ans[i][j] == '.':
elif ans[i][j] == ".":
q.append(i)
elif q:
ans[q.popleft()][j] = '#'
ans[i][j] = '.'
ans[q.popleft()][j] = "#"
ans[i][j] = "."
q.append(i)
return ans
```
Expand All @@ -129,12 +131,12 @@ class Solution:

```java
class Solution {
public char[][] rotateTheBox(char[][] box) {
int m = box.length, n = box[0].length;
public char[][] rotateTheBox(char[][] boxGrid) {
int m = boxGrid.length, n = boxGrid[0].length;
char[][] ans = new char[n][m];
for (int i = 0; i < m; ++i) {
for (int j = 0; j < n; ++j) {
ans[j][m - i - 1] = box[i][j];
ans[j][m - i - 1] = boxGrid[i][j];
}
}
for (int j = 0; j < m; ++j) {
Expand All @@ -161,12 +163,12 @@ class Solution {
```cpp
class Solution {
public:
vector<vector<char>> rotateTheBox(vector<vector<char>>& box) {
int m = box.size(), n = box[0].size();
vector<vector<char>> rotateTheBox(vector<vector<char>>& boxGrid) {
int m = boxGrid.size(), n = boxGrid[0].size();
vector<vector<char>> ans(n, vector<char>(m));
for (int i = 0; i < m; ++i) {
for (int j = 0; j < n; ++j) {
ans[j][m - i - 1] = box[i][j];
ans[j][m - i - 1] = boxGrid[i][j];
}
}
for (int j = 0; j < m; ++j) {
Expand All @@ -193,15 +195,15 @@ public:
#### Go

```go
func rotateTheBox(box [][]byte) [][]byte {
m, n := len(box), len(box[0])
func rotateTheBox(boxGrid [][]byte) [][]byte {
m, n := len(boxGrid), len(boxGrid[0])
ans := make([][]byte, n)
for i := range ans {
ans[i] = make([]byte, m)
}
for i := 0; i < m; i++ {
for j := 0; j < n; j++ {
ans[j][m-i-1] = box[i][j]
ans[j][m-i-1] = boxGrid[i][j]
}
}
for j := 0; j < m; j++ {
Expand All @@ -223,6 +225,78 @@ func rotateTheBox(box [][]byte) [][]byte {
}
```

#### TypeScript

```ts
function rotateTheBox(boxGrid: string[][]): string[][] {
const m = boxGrid.length;
const n = boxGrid[0].length;
const ans: string[][] = Array.from({ length: n }, () => Array(m));

for (let i = 0; i < m; i++) {
for (let j = 0; j < n; j++) {
ans[j][m - i - 1] = boxGrid[i][j];
}
}

for (let j = 0; j < m; j++) {
const q: number[] = [];
for (let i = n - 1; i >= 0; i--) {
if (ans[i][j] === '*') {
q.length = 0;
} else if (ans[i][j] === '.') {
q.push(i);
} else if (q.length > 0) {
const t = q.shift()!;
ans[t][j] = '#';
ans[i][j] = '.';
q.push(i);
}
}
}

return ans;
}
```

#### Rust

```rust
use std::collections::VecDeque;

impl Solution {
pub fn rotate_the_box(box_grid: Vec<Vec<char>>) -> Vec<Vec<char>> {
let m: usize = box_grid.len();
let n: usize = box_grid[0].len();
let mut ans: Vec<Vec<char>> = vec![vec![' '; m]; n];

for i in 0..m {
for j in 0..n {
ans[j][m - i - 1] = box_grid[i][j];
}
}

for j in 0..m {
let mut q: VecDeque<usize> = VecDeque::new();
for i in (0..n).rev() {
if ans[i][j] == '*' {
q.clear();
} else if ans[i][j] == '.' {
q.push_back(i);
} else if !q.is_empty() {
let t = q.pop_front().unwrap();
ans[t][j] = '#';
ans[i][j] = '.';
q.push_back(i);
}
}
}

ans
}
}
```

<!-- tabs:end -->

<!-- solution:end -->
Expand Down
110 changes: 92 additions & 18 deletions solution/1800-1899/1861.Rotating the Box/README_EN.md
Original file line number Diff line number Diff line change
Expand Up @@ -93,32 +93,34 @@ tags:

### Solution 1: Queue Simulation

First, we rotate the matrix 90 degrees clockwise, then simulate the falling process of the stones in each column.
We first rotate the matrix 90 degrees clockwise, then simulate the falling process of stones in each column.

The time complexity is $O(m \times n)$, and the space complexity is $O(m \times n)$. Where $m$ and $n$ are the number of rows and columns of the matrix, respectively.
Specifically, we use a queue $q$ to store the row indices of empty positions in the current column. When traversing each column, we scan from bottom to top. If we encounter a stone, we drop it to the first empty position in $q$, remove that empty position from $q$, and add the current position's row index to $q$ since it becomes empty. If we encounter an obstacle, we clear $q$ because stones cannot pass through obstacles. If we encounter an empty position, we add its row index to $q$.

The time complexity is $O(m \times n)$, and the space complexity is $O(m \times n)$, where $m$ and $n$ are the number of rows and columns of the matrix respectively.

<!-- tabs:start -->

#### Python3

```python
class Solution:
def rotateTheBox(self, box: List[List[str]]) -> List[List[str]]:
m, n = len(box), len(box[0])
def rotateTheBox(self, boxGrid: List[List[str]]) -> List[List[str]]:
m, n = len(boxGrid), len(boxGrid[0])
ans = [[None] * m for _ in range(n)]
for i in range(m):
for j in range(n):
ans[j][m - i - 1] = box[i][j]
ans[j][m - i - 1] = boxGrid[i][j]
for j in range(m):
q = deque()
for i in range(n - 1, -1, -1):
if ans[i][j] == '*':
if ans[i][j] == "*":
q.clear()
elif ans[i][j] == '.':
elif ans[i][j] == ".":
q.append(i)
elif q:
ans[q.popleft()][j] = '#'
ans[i][j] = '.'
ans[q.popleft()][j] = "#"
ans[i][j] = "."
q.append(i)
return ans
```
Expand All @@ -127,12 +129,12 @@ class Solution:

```java
class Solution {
public char[][] rotateTheBox(char[][] box) {
int m = box.length, n = box[0].length;
public char[][] rotateTheBox(char[][] boxGrid) {
int m = boxGrid.length, n = boxGrid[0].length;
char[][] ans = new char[n][m];
for (int i = 0; i < m; ++i) {
for (int j = 0; j < n; ++j) {
ans[j][m - i - 1] = box[i][j];
ans[j][m - i - 1] = boxGrid[i][j];
}
}
for (int j = 0; j < m; ++j) {
Expand All @@ -159,12 +161,12 @@ class Solution {
```cpp
class Solution {
public:
vector<vector<char>> rotateTheBox(vector<vector<char>>& box) {
int m = box.size(), n = box[0].size();
vector<vector<char>> rotateTheBox(vector<vector<char>>& boxGrid) {
int m = boxGrid.size(), n = boxGrid[0].size();
vector<vector<char>> ans(n, vector<char>(m));
for (int i = 0; i < m; ++i) {
for (int j = 0; j < n; ++j) {
ans[j][m - i - 1] = box[i][j];
ans[j][m - i - 1] = boxGrid[i][j];
}
}
for (int j = 0; j < m; ++j) {
Expand All @@ -191,15 +193,15 @@ public:
#### Go

```go
func rotateTheBox(box [][]byte) [][]byte {
m, n := len(box), len(box[0])
func rotateTheBox(boxGrid [][]byte) [][]byte {
m, n := len(boxGrid), len(boxGrid[0])
ans := make([][]byte, n)
for i := range ans {
ans[i] = make([]byte, m)
}
for i := 0; i < m; i++ {
for j := 0; j < n; j++ {
ans[j][m-i-1] = box[i][j]
ans[j][m-i-1] = boxGrid[i][j]
}
}
for j := 0; j < m; j++ {
Expand All @@ -221,6 +223,78 @@ func rotateTheBox(box [][]byte) [][]byte {
}
```

#### TypeScript

```ts
function rotateTheBox(boxGrid: string[][]): string[][] {
const m = boxGrid.length;
const n = boxGrid[0].length;
const ans: string[][] = Array.from({ length: n }, () => Array(m));

for (let i = 0; i < m; i++) {
for (let j = 0; j < n; j++) {
ans[j][m - i - 1] = boxGrid[i][j];
}
}

for (let j = 0; j < m; j++) {
const q: number[] = [];
for (let i = n - 1; i >= 0; i--) {
if (ans[i][j] === '*') {
q.length = 0;
} else if (ans[i][j] === '.') {
q.push(i);
} else if (q.length > 0) {
const t = q.shift()!;
ans[t][j] = '#';
ans[i][j] = '.';
q.push(i);
}
}
}

return ans;
}
```

#### Rust

```rust
use std::collections::VecDeque;

impl Solution {
pub fn rotate_the_box(box_grid: Vec<Vec<char>>) -> Vec<Vec<char>> {
let m: usize = box_grid.len();
let n: usize = box_grid[0].len();
let mut ans: Vec<Vec<char>> = vec![vec![' '; m]; n];

for i in 0..m {
for j in 0..n {
ans[j][m - i - 1] = box_grid[i][j];
}
}

for j in 0..m {
let mut q: VecDeque<usize> = VecDeque::new();
for i in (0..n).rev() {
if ans[i][j] == '*' {
q.clear();
} else if ans[i][j] == '.' {
q.push_back(i);
} else if !q.is_empty() {
let t = q.pop_front().unwrap();
ans[t][j] = '#';
ans[i][j] = '.';
q.push_back(i);
}
}
}

ans
}
}
```

<!-- tabs:end -->

<!-- solution:end -->
Expand Down
8 changes: 4 additions & 4 deletions solution/1800-1899/1861.Rotating the Box/Solution.cpp
Original file line number Diff line number Diff line change
@@ -1,11 +1,11 @@
class Solution {
public:
vector<vector<char>> rotateTheBox(vector<vector<char>>& box) {
int m = box.size(), n = box[0].size();
vector<vector<char>> rotateTheBox(vector<vector<char>>& boxGrid) {
int m = boxGrid.size(), n = boxGrid[0].size();
vector<vector<char>> ans(n, vector<char>(m));
for (int i = 0; i < m; ++i) {
for (int j = 0; j < n; ++j) {
ans[j][m - i - 1] = box[i][j];
ans[j][m - i - 1] = boxGrid[i][j];
}
}
for (int j = 0; j < m; ++j) {
Expand All @@ -26,4 +26,4 @@ class Solution {
}
return ans;
}
};
};
8 changes: 4 additions & 4 deletions solution/1800-1899/1861.Rotating the Box/Solution.go
Original file line number Diff line number Diff line change
@@ -1,12 +1,12 @@
func rotateTheBox(box [][]byte) [][]byte {
m, n := len(box), len(box[0])
func rotateTheBox(boxGrid [][]byte) [][]byte {
m, n := len(boxGrid), len(boxGrid[0])
ans := make([][]byte, n)
for i := range ans {
ans[i] = make([]byte, m)
}
for i := 0; i < m; i++ {
for j := 0; j < n; j++ {
ans[j][m-i-1] = box[i][j]
ans[j][m-i-1] = boxGrid[i][j]
}
}
for j := 0; j < m; j++ {
Expand All @@ -25,4 +25,4 @@ func rotateTheBox(box [][]byte) [][]byte {
}
}
return ans
}
}
Loading
Loading