Home >Backend Development >PHP Tutorial >Rotating the Box
1861. Rotating the Box
Difficulty: Medium
Topics: Array, Two Pointers, Matrix
You are given an m x n matrix of characters box representing a side-view of a box. Each cell of the box is one of the following:
The box is rotated 90 degrees clockwise, causing some of the stones to fall due to gravity. Each stone falls down until it lands on an obstacle, another stone, or the bottom of the box. Gravity does not affect the obstacles' positions, and the inertia from the box's rotation does not affect the stones' horizontal positions.
It is guaranteed that each stone in box rests on an obstacle, another stone, or the bottom of the box.
Return an n x m matrix representing the box after the rotation described above.
Example 1:
Example 2:
Example 3:
Constraints:
Hint:
Solution:
We need to follow a few distinct steps:
Rotate the box: We first rotate the matrix 90 degrees clockwise. The rotated matrix will have n rows and m columns, where n is the number of columns in the original box, and m is the number of rows.
Gravity effect: After rotating, we need to simulate the effect of gravity. This means that all stones ('#') should "fall" to the bottom of their new column, stopping only when they encounter an obstacle ('*') or another stone ('#').
Rotation: After the rotation, the element at position [i][j] in the original matrix will be placed at position [j][m-1-i] in the rotated matrix.
Gravity simulation: We need to process each column from bottom to top. If there is a stone ('#'), it will fall down until it reaches an obstacle or the bottom. If the cell is empty ('.'), it can hold a stone.
Let's implement this solution in PHP: 1861. Rotating the Box
<?php function rotateTheBox($box) { ... ... ... /** * go to ./solution.php */ } // Example Usage $box = [ ["#", ".", "#"], ]; print_r(rotateTheBox($box)); $box = [ ["#", ".", "*", "."], ["#", "#", "*", "."], ]; print_r(rotateTheBox($box)); $box = [ ["#", "#", "*", ".", "*", "."], ["#", "#", "#", "*", ".", "."], ["#", "#", "#", ".", "#", "."], ]; print_r(rotateTheBox($box)); ?> <h3> Explanation: </h3> <ol> <li> <p><strong>Simulate Gravity</strong>:</p> <ul> <li>Traverse each row from right to left. Use a pointer (emptySlot) to track where the next stone should fall.</li> <li>If a stone (#) is encountered, move it to the emptySlot, and then decrement emptySlot.</li> <li>If an obstacle (*) is encountered, reset emptySlot to the position just before the obstacle.</li> </ul> </li> <li> <p><strong>Rotate the Matrix</strong>:</p> <ul> <li>Create a new matrix where the element at [i][j] in the rotated box is taken from [m - 1 - j][i] of the original box.</li> </ul> </li> </ol> <h3> Example Output </h3> <h4> Input: </h4> <pre class="brush:php;toolbar:false">$box = [ ["#", ".", "#"], ];
[ [".",], ["#",], ["#",], ]
$box = [ ["#", ".", "*", "."], ["#", "#", "*", "."], ];
[ ["#", "."], ["#", "#"], ["*", "*"], [".", "."], ]
Total: O(m x n).
This solution is efficient and adheres to the constraints of the problem.
Contact Links
If you found this series helpful, please consider giving the repository a star on GitHub or sharing the post on your favorite social networks ?. Your support would mean a lot to me!
If you want more helpful content like this, feel free to follow me:
The above is the detailed content of Rotating the Box. For more information, please follow other related articles on the PHP Chinese website!