Skip to content

Commit 7af4d83

Browse files
committed
feat: add solutions for lc No.1861
1 parent bc572b4 commit 7af4d83

8 files changed

Lines changed: 263 additions & 53 deletions

File tree

solution/1800-1899/1861.Rotating the Box/README.md

Lines changed: 90 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -97,6 +97,8 @@ tags:
9797

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

100+
具体地,我们使用一个队列 $q$ 来存储当前列中空位置的行号。遍历每一列,我们从下往上扫描,如果遇到一个石头,我们就将它掉落到 $q$ 中第一个空位置,并将这个空位置从 $q$ 中移除,由于当前位置变成了空位置,我们就将它的行号加入 $q$ 中;如果遇到一个障碍物,我们就清空 $q$,因为石头无法穿过障碍物;如果遇到一个空位置,我们就将它的行号加入 $q$ 中。
101+
100102
时间复杂度 $O(m \times n)$,空间复杂度 $O(m \times n)$。其中 $m$ 和 $n$ 分别是矩阵的行数和列数。
101103

102104
<!-- tabs:start -->
@@ -105,22 +107,22 @@ tags:
105107

106108
```python
107109
class Solution:
108-
def rotateTheBox(self, box: List[List[str]]) -> List[List[str]]:
109-
m, n = len(box), len(box[0])
110+
def rotateTheBox(self, boxGrid: List[List[str]]) -> List[List[str]]:
111+
m, n = len(boxGrid), len(boxGrid[0])
110112
ans = [[None] * m for _ in range(n)]
111113
for i in range(m):
112114
for j in range(n):
113-
ans[j][m - i - 1] = box[i][j]
115+
ans[j][m - i - 1] = boxGrid[i][j]
114116
for j in range(m):
115117
q = deque()
116118
for i in range(n - 1, -1, -1):
117-
if ans[i][j] == '*':
119+
if ans[i][j] == "*":
118120
q.clear()
119-
elif ans[i][j] == '.':
121+
elif ans[i][j] == ".":
120122
q.append(i)
121123
elif q:
122-
ans[q.popleft()][j] = '#'
123-
ans[i][j] = '.'
124+
ans[q.popleft()][j] = "#"
125+
ans[i][j] = "."
124126
q.append(i)
125127
return ans
126128
```
@@ -129,12 +131,12 @@ class Solution:
129131

130132
```java
131133
class Solution {
132-
public char[][] rotateTheBox(char[][] box) {
133-
int m = box.length, n = box[0].length;
134+
public char[][] rotateTheBox(char[][] boxGrid) {
135+
int m = boxGrid.length, n = boxGrid[0].length;
134136
char[][] ans = new char[n][m];
135137
for (int i = 0; i < m; ++i) {
136138
for (int j = 0; j < n; ++j) {
137-
ans[j][m - i - 1] = box[i][j];
139+
ans[j][m - i - 1] = boxGrid[i][j];
138140
}
139141
}
140142
for (int j = 0; j < m; ++j) {
@@ -161,12 +163,12 @@ class Solution {
161163
```cpp
162164
class Solution {
163165
public:
164-
vector<vector<char>> rotateTheBox(vector<vector<char>>& box) {
165-
int m = box.size(), n = box[0].size();
166+
vector<vector<char>> rotateTheBox(vector<vector<char>>& boxGrid) {
167+
int m = boxGrid.size(), n = boxGrid[0].size();
166168
vector<vector<char>> ans(n, vector<char>(m));
167169
for (int i = 0; i < m; ++i) {
168170
for (int j = 0; j < n; ++j) {
169-
ans[j][m - i - 1] = box[i][j];
171+
ans[j][m - i - 1] = boxGrid[i][j];
170172
}
171173
}
172174
for (int j = 0; j < m; ++j) {
@@ -193,15 +195,15 @@ public:
193195
#### Go
194196
195197
```go
196-
func rotateTheBox(box [][]byte) [][]byte {
197-
m, n := len(box), len(box[0])
198+
func rotateTheBox(boxGrid [][]byte) [][]byte {
199+
m, n := len(boxGrid), len(boxGrid[0])
198200
ans := make([][]byte, n)
199201
for i := range ans {
200202
ans[i] = make([]byte, m)
201203
}
202204
for i := 0; i < m; i++ {
203205
for j := 0; j < n; j++ {
204-
ans[j][m-i-1] = box[i][j]
206+
ans[j][m-i-1] = boxGrid[i][j]
205207
}
206208
}
207209
for j := 0; j < m; j++ {
@@ -223,6 +225,78 @@ func rotateTheBox(box [][]byte) [][]byte {
223225
}
224226
```
225227

228+
#### TypeScript
229+
230+
```ts
231+
function rotateTheBox(boxGrid: string[][]): string[][] {
232+
const m = boxGrid.length;
233+
const n = boxGrid[0].length;
234+
const ans: string[][] = Array.from({ length: n }, () => Array(m));
235+
236+
for (let i = 0; i < m; i++) {
237+
for (let j = 0; j < n; j++) {
238+
ans[j][m - i - 1] = boxGrid[i][j];
239+
}
240+
}
241+
242+
for (let j = 0; j < m; j++) {
243+
const q: number[] = [];
244+
for (let i = n - 1; i >= 0; i--) {
245+
if (ans[i][j] === '*') {
246+
q.length = 0;
247+
} else if (ans[i][j] === '.') {
248+
q.push(i);
249+
} else if (q.length > 0) {
250+
const t = q.shift()!;
251+
ans[t][j] = '#';
252+
ans[i][j] = '.';
253+
q.push(i);
254+
}
255+
}
256+
}
257+
258+
return ans;
259+
}
260+
```
261+
262+
#### Rust
263+
264+
```rust
265+
use std::collections::VecDeque;
266+
267+
impl Solution {
268+
pub fn rotate_the_box(box_grid: Vec<Vec<char>>) -> Vec<Vec<char>> {
269+
let m: usize = box_grid.len();
270+
let n: usize = box_grid[0].len();
271+
let mut ans: Vec<Vec<char>> = vec![vec![' '; m]; n];
272+
273+
for i in 0..m {
274+
for j in 0..n {
275+
ans[j][m - i - 1] = box_grid[i][j];
276+
}
277+
}
278+
279+
for j in 0..m {
280+
let mut q: VecDeque<usize> = VecDeque::new();
281+
for i in (0..n).rev() {
282+
if ans[i][j] == '*' {
283+
q.clear();
284+
} else if ans[i][j] == '.' {
285+
q.push_back(i);
286+
} else if !q.is_empty() {
287+
let t = q.pop_front().unwrap();
288+
ans[t][j] = '#';
289+
ans[i][j] = '.';
290+
q.push_back(i);
291+
}
292+
}
293+
}
294+
295+
ans
296+
}
297+
}
298+
```
299+
226300
<!-- tabs:end -->
227301

228302
<!-- solution:end -->

solution/1800-1899/1861.Rotating the Box/README_EN.md

Lines changed: 92 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -93,32 +93,34 @@ tags:
9393

9494
### Solution 1: Queue Simulation
9595

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

98-
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.
98+
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$.
99+
100+
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.
99101

100102
<!-- tabs:start -->
101103

102104
#### Python3
103105

104106
```python
105107
class Solution:
106-
def rotateTheBox(self, box: List[List[str]]) -> List[List[str]]:
107-
m, n = len(box), len(box[0])
108+
def rotateTheBox(self, boxGrid: List[List[str]]) -> List[List[str]]:
109+
m, n = len(boxGrid), len(boxGrid[0])
108110
ans = [[None] * m for _ in range(n)]
109111
for i in range(m):
110112
for j in range(n):
111-
ans[j][m - i - 1] = box[i][j]
113+
ans[j][m - i - 1] = boxGrid[i][j]
112114
for j in range(m):
113115
q = deque()
114116
for i in range(n - 1, -1, -1):
115-
if ans[i][j] == '*':
117+
if ans[i][j] == "*":
116118
q.clear()
117-
elif ans[i][j] == '.':
119+
elif ans[i][j] == ".":
118120
q.append(i)
119121
elif q:
120-
ans[q.popleft()][j] = '#'
121-
ans[i][j] = '.'
122+
ans[q.popleft()][j] = "#"
123+
ans[i][j] = "."
122124
q.append(i)
123125
return ans
124126
```
@@ -127,12 +129,12 @@ class Solution:
127129

128130
```java
129131
class Solution {
130-
public char[][] rotateTheBox(char[][] box) {
131-
int m = box.length, n = box[0].length;
132+
public char[][] rotateTheBox(char[][] boxGrid) {
133+
int m = boxGrid.length, n = boxGrid[0].length;
132134
char[][] ans = new char[n][m];
133135
for (int i = 0; i < m; ++i) {
134136
for (int j = 0; j < n; ++j) {
135-
ans[j][m - i - 1] = box[i][j];
137+
ans[j][m - i - 1] = boxGrid[i][j];
136138
}
137139
}
138140
for (int j = 0; j < m; ++j) {
@@ -159,12 +161,12 @@ class Solution {
159161
```cpp
160162
class Solution {
161163
public:
162-
vector<vector<char>> rotateTheBox(vector<vector<char>>& box) {
163-
int m = box.size(), n = box[0].size();
164+
vector<vector<char>> rotateTheBox(vector<vector<char>>& boxGrid) {
165+
int m = boxGrid.size(), n = boxGrid[0].size();
164166
vector<vector<char>> ans(n, vector<char>(m));
165167
for (int i = 0; i < m; ++i) {
166168
for (int j = 0; j < n; ++j) {
167-
ans[j][m - i - 1] = box[i][j];
169+
ans[j][m - i - 1] = boxGrid[i][j];
168170
}
169171
}
170172
for (int j = 0; j < m; ++j) {
@@ -191,15 +193,15 @@ public:
191193
#### Go
192194
193195
```go
194-
func rotateTheBox(box [][]byte) [][]byte {
195-
m, n := len(box), len(box[0])
196+
func rotateTheBox(boxGrid [][]byte) [][]byte {
197+
m, n := len(boxGrid), len(boxGrid[0])
196198
ans := make([][]byte, n)
197199
for i := range ans {
198200
ans[i] = make([]byte, m)
199201
}
200202
for i := 0; i < m; i++ {
201203
for j := 0; j < n; j++ {
202-
ans[j][m-i-1] = box[i][j]
204+
ans[j][m-i-1] = boxGrid[i][j]
203205
}
204206
}
205207
for j := 0; j < m; j++ {
@@ -221,6 +223,78 @@ func rotateTheBox(box [][]byte) [][]byte {
221223
}
222224
```
223225

226+
#### TypeScript
227+
228+
```ts
229+
function rotateTheBox(boxGrid: string[][]): string[][] {
230+
const m = boxGrid.length;
231+
const n = boxGrid[0].length;
232+
const ans: string[][] = Array.from({ length: n }, () => Array(m));
233+
234+
for (let i = 0; i < m; i++) {
235+
for (let j = 0; j < n; j++) {
236+
ans[j][m - i - 1] = boxGrid[i][j];
237+
}
238+
}
239+
240+
for (let j = 0; j < m; j++) {
241+
const q: number[] = [];
242+
for (let i = n - 1; i >= 0; i--) {
243+
if (ans[i][j] === '*') {
244+
q.length = 0;
245+
} else if (ans[i][j] === '.') {
246+
q.push(i);
247+
} else if (q.length > 0) {
248+
const t = q.shift()!;
249+
ans[t][j] = '#';
250+
ans[i][j] = '.';
251+
q.push(i);
252+
}
253+
}
254+
}
255+
256+
return ans;
257+
}
258+
```
259+
260+
#### Rust
261+
262+
```rust
263+
use std::collections::VecDeque;
264+
265+
impl Solution {
266+
pub fn rotate_the_box(box_grid: Vec<Vec<char>>) -> Vec<Vec<char>> {
267+
let m: usize = box_grid.len();
268+
let n: usize = box_grid[0].len();
269+
let mut ans: Vec<Vec<char>> = vec![vec![' '; m]; n];
270+
271+
for i in 0..m {
272+
for j in 0..n {
273+
ans[j][m - i - 1] = box_grid[i][j];
274+
}
275+
}
276+
277+
for j in 0..m {
278+
let mut q: VecDeque<usize> = VecDeque::new();
279+
for i in (0..n).rev() {
280+
if ans[i][j] == '*' {
281+
q.clear();
282+
} else if ans[i][j] == '.' {
283+
q.push_back(i);
284+
} else if !q.is_empty() {
285+
let t = q.pop_front().unwrap();
286+
ans[t][j] = '#';
287+
ans[i][j] = '.';
288+
q.push_back(i);
289+
}
290+
}
291+
}
292+
293+
ans
294+
}
295+
}
296+
```
297+
224298
<!-- tabs:end -->
225299

226300
<!-- solution:end -->

solution/1800-1899/1861.Rotating the Box/Solution.cpp

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,11 @@
11
class Solution {
22
public:
3-
vector<vector<char>> rotateTheBox(vector<vector<char>>& box) {
4-
int m = box.size(), n = box[0].size();
3+
vector<vector<char>> rotateTheBox(vector<vector<char>>& boxGrid) {
4+
int m = boxGrid.size(), n = boxGrid[0].size();
55
vector<vector<char>> ans(n, vector<char>(m));
66
for (int i = 0; i < m; ++i) {
77
for (int j = 0; j < n; ++j) {
8-
ans[j][m - i - 1] = box[i][j];
8+
ans[j][m - i - 1] = boxGrid[i][j];
99
}
1010
}
1111
for (int j = 0; j < m; ++j) {
@@ -26,4 +26,4 @@ class Solution {
2626
}
2727
return ans;
2828
}
29-
};
29+
};

solution/1800-1899/1861.Rotating the Box/Solution.go

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,12 @@
1-
func rotateTheBox(box [][]byte) [][]byte {
2-
m, n := len(box), len(box[0])
1+
func rotateTheBox(boxGrid [][]byte) [][]byte {
2+
m, n := len(boxGrid), len(boxGrid[0])
33
ans := make([][]byte, n)
44
for i := range ans {
55
ans[i] = make([]byte, m)
66
}
77
for i := 0; i < m; i++ {
88
for j := 0; j < n; j++ {
9-
ans[j][m-i-1] = box[i][j]
9+
ans[j][m-i-1] = boxGrid[i][j]
1010
}
1111
}
1212
for j := 0; j < m; j++ {
@@ -25,4 +25,4 @@ func rotateTheBox(box [][]byte) [][]byte {
2525
}
2626
}
2727
return ans
28-
}
28+
}

0 commit comments

Comments
 (0)