You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Copy file name to clipboardExpand all lines: solution/1800-1899/1861.Rotating the Box/README_EN.md
+92-18Lines changed: 92 additions & 18 deletions
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -93,32 +93,34 @@ tags:
93
93
94
94
### Solution 1: Queue Simulation
95
95
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.
97
97
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.
0 commit comments