Tap here to switch tabs
Problem
Submissions

Problem: Rotating the Box

Rotating the Box takes a character grid of stones, obstacles, and empty spaces, then returns the grid after a clockwise quarter-turn, with stones falling toward the new bottom.

Statement

You are given an m×nm \times n matrix of characters boxGrid representing a side-view of a box. Each cell in the box contains one of the following:

  • A stone '#'

  • A stationary obstacle '*'

  • An empty space '.'

The box is rotated 9090 degrees clockwise, causing stones to fall due to gravity. Each stone falls downward until it lands on an obstacle, another stone, or the bottom of the box. Gravity does not affect obstacle positions, and the rotation’s inertia does not affect stones’ horizontal positions.

It is guaranteed that every stone in boxGrid initially rests on an obstacle, another stone, or the bottom of the box.

Return an n×mn \times m matrix representing the box after the rotation described above.

Note: Gravity acts after the rotation, so stones fall in the direction of the new bottom (i.e., toward the last column of the original orientation).

Constraints:

  • m == boxGrid.length

  • n == boxGrid[i].length

  • 1≤1 \leq m, n ≤500\leq 500

  • boxGrid[i][j] is either '#', '*', or '.'

⋮
Tap here to switch tabs
Problem
Submissions

Problem: Rotating the Box

Rotating the Box takes a character grid of stones, obstacles, and empty spaces, then returns the grid after a clockwise quarter-turn, with stones falling toward the new bottom.

Statement

You are given an m×nm \times n matrix of characters boxGrid representing a side-view of a box. Each cell in the box contains one of the following:

  • A stone '#'

  • A stationary obstacle '*'

  • An empty space '.'

The box is rotated 9090 degrees clockwise, causing stones to fall due to gravity. Each stone falls downward until it lands on an obstacle, another stone, or the bottom of the box. Gravity does not affect obstacle positions, and the rotation’s inertia does not affect stones’ horizontal positions.

It is guaranteed that every stone in boxGrid initially rests on an obstacle, another stone, or the bottom of the box.

Return an n×mn \times m matrix representing the box after the rotation described above.

Note: Gravity acts after the rotation, so stones fall in the direction of the new bottom (i.e., toward the last column of the original orientation).

Constraints:

  • m == boxGrid.length

  • n == boxGrid[i].length

  • 1≤1 \leq m, n ≤500\leq 500

  • boxGrid[i][j] is either '#', '*', or '.'