Tap here to switch tabs
Problem
Ask
Submissions

Problem: Rotating the Box

med
30 min
Understand how to apply the two pointers technique to simulate the rotation of a box matrix and the effect of gravity on stones. This lesson helps you manipulate 2D arrays by implementing stone movement after rotation, develop efficient algorithms for grid problems, and reinforce problem-solving skills with obstacles and gravity constraints.

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

  • 11 \leq m, n 500\leq 500

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

Tap here to switch tabs
Problem
Ask
Submissions

Problem: Rotating the Box

med
30 min
Understand how to apply the two pointers technique to simulate the rotation of a box matrix and the effect of gravity on stones. This lesson helps you manipulate 2D arrays by implementing stone movement after rotation, develop efficient algorithms for grid problems, and reinforce problem-solving skills with obstacles and gravity constraints.

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

  • 11 \leq m, n 500\leq 500

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