You are given an m x n matrix of characters box that shows a side view of a box. Each cell is one of the following:
-
a stone
'#'
-
a stationary obstacle
'*'
-
an empty cell
'.'
The box is rotated 90 degrees clockwise, so each former row becomes a column. After the rotation, gravity makes every stone fall down until it lands on an obstacle, another stone, or the bottom of the box.
Return the n x m matrix that shows the box after the rotation and after the stones have settled.
Function Signature
def rotate_the_box(box: list[list[str]]) -> list[list[str]]:
Rules
-
A 90-degree clockwise rotation sends the cell at row
i
, column
j
of the original box to row
j
, column
m - 1 - i
of the result.
-
Obstacles rotate with the box but never move relative to it. Only stones fall, and each stone falls straight down within its own column.
-
Stones are identical, so the result is unique: in each column, every run of cells between obstacles (or between an obstacle and the top or bottom edge) keeps its number of stones, packed at the bottom of the run.
-
Every cell of the result is one of
'#'
,
'*'
or
'.'
.
Constraints
-
m == len(box)
-
n == len(box[i])
-
1 <= m, n <= 500
-
box[i][j]
is
'#'
,
'*'
or
'.'
Examples
Input: box = [["#",".","#"]]
Output: [["."],["#"],["#"]]
The single row becomes a single column, read top to bottom as #, ., #. The top stone falls one cell and comes to rest on the other stone.
Input: box = [["#",".","*","."],["#","#","*","."]]
Output: [["#","."],["#","#"],["*","*"],[".","."]]
The second original row becomes the left column (#, #, *, .). Its two stones already sit on the obstacle, so nothing moves. The first row becomes the right column (#, ., *, .), and its stone falls one cell onto the obstacle. The bottom cells stay empty because no stone can pass an obstacle.
Input: box = [["#",".","#","*","#","."],[".","#","*",".",".","#"]]
Output: [[".","."],["#","#"],["*","#"],[".","*"],[".","."],["#","#"]]
The right column comes from the first row: above its obstacle, the two stones in #, ., # settle to ., #, #, and below it the stone in #, . drops to the floor. The left column comes from the second row, and its stones already rest on the obstacle and on the floor.