diff --git a/solution/1800-1899/1861.Rotating the Box/README.md b/solution/1800-1899/1861.Rotating the Box/README.md index 582727d21134d..4ee934b47cfa1 100644 --- a/solution/1800-1899/1861.Rotating the Box/README.md +++ b/solution/1800-1899/1861.Rotating the Box/README.md @@ -97,6 +97,8 @@ tags: 我们先将矩阵顺时针旋转 90 度,然后模拟每一列石头的下落过程。 +具体地,我们使用一个队列 $q$ 来存储当前列中空位置的行号。遍历每一列,我们从下往上扫描,如果遇到一个石头,我们就将它掉落到 $q$ 中第一个空位置,并将这个空位置从 $q$ 中移除,由于当前位置变成了空位置,我们就将它的行号加入 $q$ 中;如果遇到一个障碍物,我们就清空 $q$,因为石头无法穿过障碍物;如果遇到一个空位置,我们就将它的行号加入 $q$ 中。 + 时间复杂度 $O(m \times n)$,空间复杂度 $O(m \times n)$。其中 $m$ 和 $n$ 分别是矩阵的行数和列数。 @@ -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 ``` @@ -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) { @@ -161,12 +163,12 @@ class Solution { ```cpp class Solution { public: - vector> rotateTheBox(vector>& box) { - int m = box.size(), n = box[0].size(); + vector> rotateTheBox(vector>& boxGrid) { + int m = boxGrid.size(), n = boxGrid[0].size(); vector> ans(n, vector(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) { @@ -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++ { @@ -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> { + let m: usize = box_grid.len(); + let n: usize = box_grid[0].len(); + let mut ans: Vec> = 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 = 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 + } +} +``` + diff --git a/solution/1800-1899/1861.Rotating the Box/README_EN.md b/solution/1800-1899/1861.Rotating the Box/README_EN.md index 3c58cf3f9ab05..3a977a4ae63cf 100644 --- a/solution/1800-1899/1861.Rotating the Box/README_EN.md +++ b/solution/1800-1899/1861.Rotating the Box/README_EN.md @@ -93,9 +93,11 @@ 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. @@ -103,22 +105,22 @@ The time complexity is $O(m \times n)$, and the space complexity is $O(m \times ```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 ``` @@ -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) { @@ -159,12 +161,12 @@ class Solution { ```cpp class Solution { public: - vector> rotateTheBox(vector>& box) { - int m = box.size(), n = box[0].size(); + vector> rotateTheBox(vector>& boxGrid) { + int m = boxGrid.size(), n = boxGrid[0].size(); vector> ans(n, vector(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) { @@ -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++ { @@ -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> { + let m: usize = box_grid.len(); + let n: usize = box_grid[0].len(); + let mut ans: Vec> = 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 = 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 + } +} +``` + diff --git a/solution/1800-1899/1861.Rotating the Box/Solution.cpp b/solution/1800-1899/1861.Rotating the Box/Solution.cpp index 92a21a463bbd6..b5eafb7c7a44a 100644 --- a/solution/1800-1899/1861.Rotating the Box/Solution.cpp +++ b/solution/1800-1899/1861.Rotating the Box/Solution.cpp @@ -1,11 +1,11 @@ class Solution { public: - vector> rotateTheBox(vector>& box) { - int m = box.size(), n = box[0].size(); + vector> rotateTheBox(vector>& boxGrid) { + int m = boxGrid.size(), n = boxGrid[0].size(); vector> ans(n, vector(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) { @@ -26,4 +26,4 @@ class Solution { } return ans; } -}; \ No newline at end of file +}; diff --git a/solution/1800-1899/1861.Rotating the Box/Solution.go b/solution/1800-1899/1861.Rotating the Box/Solution.go index 306f23b684ea0..c524c205297c3 100644 --- a/solution/1800-1899/1861.Rotating the Box/Solution.go +++ b/solution/1800-1899/1861.Rotating the Box/Solution.go @@ -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++ { @@ -25,4 +25,4 @@ func rotateTheBox(box [][]byte) [][]byte { } } return ans -} \ No newline at end of file +} diff --git a/solution/1800-1899/1861.Rotating the Box/Solution.java b/solution/1800-1899/1861.Rotating the Box/Solution.java index 729c8893c4f58..9d6a1a60b2ef4 100644 --- a/solution/1800-1899/1861.Rotating the Box/Solution.java +++ b/solution/1800-1899/1861.Rotating the Box/Solution.java @@ -1,10 +1,10 @@ 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) { @@ -23,4 +23,4 @@ public char[][] rotateTheBox(char[][] box) { } return ans; } -} \ No newline at end of file +} diff --git a/solution/1800-1899/1861.Rotating the Box/Solution.py b/solution/1800-1899/1861.Rotating the Box/Solution.py index 2a97afba5513c..b1908d3cb0d19 100644 --- a/solution/1800-1899/1861.Rotating the Box/Solution.py +++ b/solution/1800-1899/1861.Rotating the Box/Solution.py @@ -1,19 +1,19 @@ 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 diff --git a/solution/1800-1899/1861.Rotating the Box/Solution.rs b/solution/1800-1899/1861.Rotating the Box/Solution.rs new file mode 100644 index 0000000000000..bb6c7f51001a0 --- /dev/null +++ b/solution/1800-1899/1861.Rotating the Box/Solution.rs @@ -0,0 +1,33 @@ +use std::collections::VecDeque; + +impl Solution { + pub fn rotate_the_box(box_grid: Vec>) -> Vec> { + let m: usize = box_grid.len(); + let n: usize = box_grid[0].len(); + let mut ans: Vec> = 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 = 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 + } +} diff --git a/solution/1800-1899/1861.Rotating the Box/Solution.ts b/solution/1800-1899/1861.Rotating the Box/Solution.ts new file mode 100644 index 0000000000000..9ba43d0336330 --- /dev/null +++ b/solution/1800-1899/1861.Rotating the Box/Solution.ts @@ -0,0 +1,29 @@ +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; +}