Leetcode Problem 1861. Rotating the Box

1861. Rotating the Box

Leetcode Solutions

Gravity Simulation and Matrix Rotation

  1. Initialize a new matrix result with dimensions swapped (n x m).
  2. For each row in the original matrix box, process from right to left.
  3. Use two pointers: one to track the current position (current) and another to track the position where the next stone should fall (nextStone).
  4. If an obstacle is encountered, set nextStone to the position just left of the obstacle.
  5. If a stone is encountered, move it to the nextStone position and decrement nextStone.
  6. After processing a row, fill the rest of the row in result with '.' up to the nextStone position.
  7. Rotate the box by assigning values from box to result with adjusted indices to account for the rotation.
  8. Return the result matrix.
UML Thumbnail

In-place Gravity Simulation with Post-rotation

Ask Question

Programming Language
image/screenshot of info(optional)
Full Screen
Loading...

Suggested Answer

Answer
Full Screen
Copy Answer Code
Loading...