1861. Rotating the Box
MD ARIFUL HAQUE
Posted on November 23, 2024
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:
- A stone
'#'
- A stationary obstacle
'*'
- Empty
'.'
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:
- Input: box = [["#",".","#"]]
- Output: [["."], ["#"], ["#"]]
Example 2:
- Input: box = [["#",".","","."], ["#","#","","."]]
- Output: [["#","."], ["#","#"], ["",""], [".","."]]
Example 3:
- Input: box = [["#","#","",".","","."], ["#","#","#","*",".","."], ["#","#","#",".","#","."]]
- Output: [[".","#","#"], [".","#","#"], ["#","#",""], ["#","","."], ["#",".","*"], ["#",".","."]]
Constraints:
m == box.length
n == box[i].length
1 <= m, n <= 500
-
box[i][j]
is either'#'
,'*'
, or'.'
.
Hint:
- Rotate the box using the relation
rotatedBox[i][j] = box[m - 1 - j][i]
. - Start iterating from the bottom of the box and for each empty cell check if there is any stone above it with no obstacles between them.
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 andm
columns, wheren
is the number of columns in the original box, andm
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 ('#'
).
Approach:
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.
Step-by-step explanation:
- Create a new matrix for the rotated box.
- Iterate through each column of the rotated matrix (after rotation).
-
Simulate gravity for each column by starting from the bottom and moving upwards. Place stones (
'#'
) as far down as possible, leaving obstacles ('*'
) in place. - Return the final rotated matrix.
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));
?>
Explanation:
-
Simulate Gravity:
- Traverse each row from right to left. Use a pointer (
emptySlot
) to track where the next stone should fall. - If a stone (
#
) is encountered, move it to theemptySlot
, and then decrementemptySlot
. - If an obstacle (
*
) is encountered, resetemptySlot
to the position just before the obstacle.
- Traverse each row from right to left. Use a pointer (
-
Rotate the Matrix:
- 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.
- Create a new matrix where the element at
Example Output
Input:
$box = [
["#", ".", "#"],
];
Output:
[
[".",],
["#",],
["#",],
]
Input:
$box = [
["#", ".", "*", "."],
["#", "#", "*", "."],
];
Output:
[
["#", "."],
["#", "#"],
["*", "*"],
[".", "."],
]
Time Complexity
- Gravity simulation: O(m x n), as we iterate through each element in the matrix.
- Rotation: O(m x n), as we create the rotated matrix.
Total: O(m x n).
Space Complexity
- O(m x n) for the rotated matrix.
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:
Posted on November 23, 2024
Join Our Newsletter. No Spam, Only the good stuff.
Sign up to receive the latest update from our blog.