#1861

Rotating the Box

pupil · 450 · lc medium +26 · verified · 79.2% accepted · 1,625 likes · top 90%

Description

You are given an m x n character matrix boxGrid containing '#' (stone), '*' (obstacle), and '.' (empty). Rotating the box 90 degrees clockwise causes gravity to act downward on the stones: each stone falls until it hits an obstacle, another stone, or the bottom wall. Obstacles stay fixed.

Return the resulting n x m matrix after rotation.

Example 1:

Input: boxGrid = [["#",".","#"]]
Output: [["."],
["#"],
["#"]]

Example 2:

Input: boxGrid = [["#",".","*","."],
["#","#","*","."]]
Output: [["#","."],
["#","#"],
["*","*"],
[".","."]]

Example 3:

Input: boxGrid = [["#","#","*",".","*","."],
["#","#","#","*",".","."],
["#","#","#",".","#","."]]
Output: [[".","#","#"],
[".","#","#"],
["#","#","*"],
["#","*","."],
["#",".","*"],
["#",".","."]]

Code

1
2
3